IOS 设计单例实例 AFNetworking 3 时出错

IOS error in design singleton instance AFNetworking 3

嗨,我正在为 AFNetworking 3.x

构建一个单例实例

我的ApiClient.h

@interface ApiClient : AFHTTPSessionManager
+ (ApiClient *)sharedInstance;

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration;

我的ApiClient.m

+ (ApiClient *)sharedInstance {
    static ApiClient *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:SINGPOST_BASE_URL] sessionConfiguration:sessionConfiguration];

    });
return sharedInstance;
}

问题是我无法启动单例实例

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {

    [super initWithBaseURL:url sessionConfiguration:url];
    return self;
}

我有 2 个错误,如下图

谁知道。我的单例设计有什么问题?非常感谢任何帮助!谢谢

PS:我试过 initwithURL 只有在没有会话配置的情况下无法工作

更新。有错别字。正确的应该是

-(instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
    self = [super initWithBaseURL:url sessionConfiguration:configuration];

        if (self) {
//            self.responseSerializer = [AFHTTPResponseSerializer serializer];
//            self.requestSerializer = [AFHTTPRequestSerializer serializer];
            [self setDataTaskWillCacheResponseBlock:^NSCachedURLResponse *(NSURLSession *session, NSURLSessionDataTask *dataTask, NSCachedURLResponse *proposedResponse)
             {
                 NSHTTPURLResponse *resp = (NSHTTPURLResponse*)proposedResponse.response;
                 NSMutableDictionary *newHeaders = [[resp allHeaderFields] mutableCopy];
                 if (newHeaders[@"Cache-Control"] == nil) {
                     newHeaders[@"Cache-Control"] = @"max-age=120, public";
                 }

                 //             NSHTTPURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:@"1.1" headerFields:newHeaders];

                 NSURLResponse *response2 = [[NSHTTPURLResponse alloc] initWithURL:resp.URL statusCode:resp.statusCode HTTPVersion:nil headerFields:newHeaders];
                 NSCachedURLResponse *cachedResponse2 = [[NSCachedURLResponse alloc] initWithResponse:response2
                                                                                                 data:[proposedResponse data]
                                                                                             userInfo:[proposedResponse userInfo]
                                                                                        storagePolicy:NSURLCacheStorageAllowed];
                 return cachedResponse2;
             }];
        }

    return self;
}

它现在工作正常,但每当我再次尝试调用 viewcontroller 时。响应序列化程序无法正常工作(第二次调用)有不正确的响应,而第一次调用给出正确的响应序列化程序)。结果导致应用程序崩溃

我对 viewcontroller 第二次通话的奇怪回应

实现init方法时,要说self = ...,例如

- (instancetype)initWithBaseURL:(NSURL *)url sessionConfiguration:(NSURLSessionConfiguration *)configuration {
    self = [super initWithBaseURL:url sessionConfiguration:configuration];

    if (self) {
        // do additional configuration here
    }

    return self;
}