【发布时间】:2015-04-10 05:18:29
【问题描述】:
我刚刚升级到 Xcode 6.3 和 Swift 1.2,并且正在更新语法。在我的项目中,我使用TCBlobDownloadSwift 进行一些常规下载任务。更新后,我从框架的以下方法中得到一个编译器错误“'NSError' is not convertible to 'NSError?'”:
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError sessionError: NSError?) {
let download = self.downloads[task.taskIdentifier]!
var error: NSError? = sessionError ?? download.error
// Handle possible HTTP errors
if let response = task.response as? NSHTTPURLResponse {
// NSURLErrorDomain errors are not supposed to be reported by this delegate
// according to https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html
// so let's ignore them as they sometimes appear there for now. (But WTF?)
if !validateResponse(response) && (error == nil || error!.domain == NSURLErrorDomain) {
error = NSError(domain: kTCBlobDownloadErrorDomain,
code: TCBlobDownloadError.TCBlobDownloadHTTPError.rawValue,
userInfo: [kTCBlobDownloadErrorDescriptionKey: "Erroneous HTTP status code: \(response.statusCode)",
kTCBlobDownloadErrorFailingURLKey: task.originalRequest.URL,
kTCBlobDownloadErrorHTTPStatusKey: response.statusCode])
}
}
// Remove reference to the download
self.downloads.removeValueForKey(task.taskIdentifier)
dispatch_async(dispatch_get_main_queue()) {
download.delegate?.download(download, didFinishWithError: error, atLocation: download.resultingURL)
return
}
}
}
不知何故,我不能再将NSError(用initWithDomain:code:userInfo 初始化)分配给error(NSError?)。 Swift 1.2 发生了什么变化?我应该如何解决这个问题?
在网络相关函数中,返回NSError? 似乎是正常的,尤其是在使用 objC 库的情况下。我现在对携带错误的最佳方式感到困惑,我应该使用NSError、NSError? 还是NSErrorPointer?
【问题讨论】: