简体   繁体   English

如果NSURLSession#dataTaskWithRequest返回nil是什么意思?

[英]What does it mean if NSURLSession#dataTaskWithRequest returns nil?

Starting in iOS 9.0, NSURLSession#dataTaskWithRequest may return NSURLSessionDataTask? 从iOS 9.0开始, NSURLSession#dataTaskWithRequest可能返回NSURLSessionDataTask? where as before it always returned a NSURLSessionDataTask . 和以前一样,它总是返回NSURLSessionDataTask The Swift migration simply suggests just adding a ! Swift迁移只是建议添加一个! to force unwrap the task. 强制展开任务。 But this still left me wondering why this change was made. 但这仍然让我想知道为什么要进行此更改。 What does it actually mean if nil was returned? 如果返回nil实际上是什么意思? Under what conditions with a task fail to be constructed? 在什么条件下无法完成任务? What's the best way to handle these errors in an application? 在应用程序中处理这些错误的最佳方法是什么?

let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
// Swift migrator suggests force unwrapping the optional
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)!

iOS 8.3 iOS 8.3

- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request
- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler

iOS 9.0 iOS 9.0

- (NSURLSessionDataTask * nullable)dataTaskWithRequest:(NSURLRequest * nonnull)request
- (NSURLSessionDataTask * nullable)dataTaskWithRequest:(NSURLRequest * nonnull)request completionHandler:(void (^ nonnull)(NSData * nullable data, NSURLResponse * nullable response, NSError * nullable error))completionHandler

https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS90APIDiffs/frameworks/Foundation.html https://developer.apple.com/library/prerelease/ios/releasenotes/General/iOS90APIDiffs/frameworks/Foundation.html

Looks like this was an error in the API. 看起来这是API中的错误。 As of now the diff linked in the question shows 截至目前,问题中链接的差异显示

- (NSURLSessionDataTask * _Nonnull)dataTaskWithRequest:(NSURLRequest * _Nonnull)request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM