Reputation: 313
I just upgraded to Xcode 6.3 and Swift 1.2, and am in the process of updating syntaxes. In my project I use TCBlobDownloadSwift for some regular downloading tasks. After the update, I get a compiler error "'NSError' is not convertible to 'NSError?'"from the following method from the framework:
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
}
}
}
Somehow I cannot assign a NSError
(initialized with initWithDomain:code:userInfo
)to error
(NSError?) anymore. What has changed in Swift 1.2? How should I fix this?
Returning a NSError?
seems to be normal among networking related functions, especially if objC libraries were used. I'm now confused about the best way to carry an error, should I use NSError
, NSError?
or NSErrorPointer
?
Upvotes: 2
Views: 1000
Reputation: 313
So I fixed the problem, turns out there's nothing wrong with assigning an optional value to a NSError. the userInfo
dictionary I pass to NSError
's init method takes a URL from NSURLSessionTask
's OriginalRequest
,which is a NSURLRequest
. this property is now changed to NSURL?
. I changed to task.originalRequest.URL!
and it worked.
I guess I will continue using NSError?
to pass down errors.
Upvotes: 1