gpt4 book ai didi

设计单例实例 AFNetworking 3 中的 IOS 错误

转载 作者:行者123 更新时间:2023-11-29 11:56:05 26 4
gpt4 key购买 nike

您好,我正在为 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 个错误,如下图 enter image description here

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

PS:我试过 initwithURL 没有 session 配置就不行

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

-(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;
}

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

我对第二次调用 viewcontroller 的奇怪 react enter image description here

最佳答案

当实现init方法时,你想说self = ...,例如

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

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

return self;
}

关于设计单例实例 AFNetworking 3 中的 IOS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39023923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com