[英]Method initializing an object in a dispatch_sync Block flagged by static analyzer as returning nil
我无法弄清楚为什么Xcode的静态分析器会向我发出警告“从预期返回非空值的方法返回的'nil'”。 方法是:
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
fromFile:(NSURL *)fileURL
progress:(NSProgress * __autoreleasing *)progress
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler {
__block NSURLSessionUploadTask *uploadTask = nil;
dispatch_sync(url_session_manager_creation_queue(), ^{
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
});
if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
}
}
[self addDelegateForUploadTask:uploadTask progress:progress completionHandler:completionHandler];
return uploadTask; // nil returned from a method that is expected to return a non-null value
}
分析器从您自己的代码中获取提示。 您的代码正在检查uploadTask
是否为nil
的事实告诉分析器(不管它可能对您所调用的方法有什么了解),它可能为nil
。 即使attempts
超过AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask
,它也有可能一直nil
。 您自己的代码说明了这一点。 而且,如果是这样,假设你的方法被声明为返回非nil
指针,然后你会侵犯您的设计合同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.