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.
我正在尝试实现错误处理,如Apple的Swift Docs的“错误报告”部分所述(https://developer.apple.com/library/mac/documentation/swift/conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html)但我必须遗失了什么。
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...
我有下面的代码(简化),即使网络请求明显失败(我与互联网断开连接,并且“ERROR”被打印),我作为指针传递的错误变量在完成块内总是为零...
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()
}
1 个解决方案
#1
0
Solution
You will find a clear example of Swift error handling in the free ebook from Apple entitled Using Swift with Cocoa and Objective-C (p.51).
您将在Apple的免费电子书中找到一个明确的Swift错误处理示例,名为Using Swift with Cocoa and Objective-C(p.51)。
var writeError: NSError?
let written = myString.writeToFile(path, atomically: false,
encoding: NSUTF8StringEncoding,
error: &writeError)
if !written {
if let error = writeError {
println("write failure: \(error.localizedDescription)")
}
}
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/us/1u3-0.l
Basically you want to look at the memory address of the error and then test that value to handle the error. Try extrapolating from this and rewrite your code to achieve the effective results.
基本上你想查看错误的内存地址,然后测试该值来处理错误。尝试从中推断并重写代码以获得有效的结果。
Also if you must use NSErrorPointer then see the following SO question and answer.
此外,如果您必须使用NSErrorPointer,请参阅以下SO问题和答案。
#1
0
Solution
You will find a clear example of Swift error handling in the free ebook from Apple entitled Using Swift with Cocoa and Objective-C (p.51).
您将在Apple的免费电子书中找到一个明确的Swift错误处理示例,名为Using Swift with Cocoa and Objective-C(p.51)。
var writeError: NSError?
let written = myString.writeToFile(path, atomically: false,
encoding: NSUTF8StringEncoding,
error: &writeError)
if !written {
if let error = writeError {
println("write failure: \(error.localizedDescription)")
}
}
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/us/1u3-0.l
Basically you want to look at the memory address of the error and then test that value to handle the error. Try extrapolating from this and rewrite your code to achieve the effective results.
基本上你想查看错误的内存地址,然后测试该值来处理错误。尝试从中推断并重写代码以获得有效的结果。
Also if you must use NSErrorPointer then see the following SO question and answer.
此外,如果您必须使用NSErrorPointer,请参阅以下SO问题和答案。