I am trying to implement error handling as explained in the 'Error Reporting' section of Apple's Swift Docs (https://developer.apple.com/library/mac/documentation/swift/conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html) but I must be missing something.
I have the code below (simplified), and even if the network request clearly fails (I am disconnected from the internet, and "ERROR" gets printed) the error variable I pass as a pointer is always nil inside the completion block...
What am I doing wrong? Thanks!
override func viewDidLoad() {
super.viewDidLoad()
var error: NSError?
// THIS IS ACTUALLY IN ANOTHER CLASS,
// BUT EVEN IF PLACED IN THE SAME CLASS I STILL GET THE SAME PROBLEM
doStuff({ (result) -> Void in
println("\(error)") // THIS PRINTS NIL
}, errorPointer: &error)
}
func doStuff(completion: (result:SomeType) -> Void, errorPointer: NSErrorPointer) {
// SETUP CODE NOT INCLUDED
let downloadTask = manager.downloadTaskWithRequest(req, progress: nil, destination: { (url, response) -> NSURL! in
return destinationURL
}) { (response, url, error) -> Void in
// CREATE RESULT
if(error != nil && errorPointer != nil) {
println(“ERROR”) // THIS GETS PRINTED
errorPointer.memory = error // ALSO TRIED NSError(...)
}
completion(result)
}
downloadTask.resume()
}