Objective c 在AFNetworking中,为什么有些返回对象声明为_块类型?
AFNetworking代码有几个地方,其中_块用于方法中的对象,在这些方法中,没有明显需要更改对象。例如,在AFHTTPSessionManager中,GET调用使用任务对象上的_块。知道为什么吗Objective c 在AFNetworking中,为什么有些返回对象声明为_块类型?,objective-c,objective-c-blocks,Objective C,Objective C Blocks,AFNetworking代码有几个地方,其中_块用于方法中的对象,在这些方法中,没有明显需要更改对象。例如,在AFHTTPSessionManager中,GET调用使用任务对象上的_块。知道为什么吗 - (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(N
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
NSMutableURLRequest *request = [self.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:self.baseURL] absoluteString] parameters:parameters];
__block NSURLSessionDataTask *task = [self dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
if (failure) {
failure(task, error);
}
} else {
if (success) {
success(task, responseObject);
}
}
}];
[task resume];
return task;
}
类似地,在其他类中,_块用于对象,如下所示用于凭证对象
- (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__block NSURLCredential *credential = nil;
if (self.sessionDidReceiveAuthenticationChallenge) {
disposition = self.sessionDidReceiveAuthenticationChallenge(session, challenge, &credential);
} else {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust]) {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
if (credential) {
disposition = NSURLSessionAuthChallengeUseCredential;
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
} else {
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
}
}
if (completionHandler) {
completionHandler(disposition, credential);
}
}
在这两个地方,
\u块
都是无用和不必要的
在第一种情况下,变量task
在定义行初始化后未分配给<代码>\u块仅当变量被分配到时才有用(并且用于MRC中的非保留目的),因此它在这里没有意义
在第二种情况下,变量
credential
根本没有被捕获到一个块中,因此它同样是无用的。这在我看来是个错误。至少第一个是这样的。