Swift 闭包中的 nil AutoreleasingUnsafeMutablePointer 有什么意义?
What is the point of a nil AutoreleasingUnsafeMutablePointer in a Swift closure?
我正在阅读 Swift 中解析 REST API 调用的不同方法并遇到以下内容:
var url : String = "http://google.com?test=toto&test2=titi"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
// process jsonResult
} else {
// couldn't load JSON, look at error
}
})
对我来说没有意义的一行是var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
。我们已经捕获了我们的 NSError
参数并将其存储在一个名为 error
的变量中,现在我们要覆盖它并在闭包的第一行中将其设为 nil?或者如果 Swift 然后以某种方式将错误从类型 NSError!
向下转换为 AutoreleasingUnsafeMutablePointer<NSError?>
,那么有人可以解释这是怎么发生的吗?
谢谢!
AutoreleasingUnsafeMutablePointer 相当于 Objective-C 中的 NSError**,在方法中用作 inout 表达式。语法看起来很奇怪。
最可靠的方法是同时考虑这两个错误并定义第二个错误变量。由于 GET 是 NSURLRequest 的默认 HTTP 方法,因此不可变请求就足够了。
let url = "http://google.com?test=toto&test2=titi"
let request = NSURLRequest(URL: NSURL(string: url)!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if error != nil {
// handle NSURLConnection error
} else {
var jsonError : NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary {
// process jsonResult
} else {
// couldn't load JSON, look at jsonError
}
}
})
我正在阅读 Swift 中解析 REST API 调用的不同方法并遇到以下内容:
var url : String = "http://google.com?test=toto&test2=titi"
var request : NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
// process jsonResult
} else {
// couldn't load JSON, look at error
}
})
对我来说没有意义的一行是var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
。我们已经捕获了我们的 NSError
参数并将其存储在一个名为 error
的变量中,现在我们要覆盖它并在闭包的第一行中将其设为 nil?或者如果 Swift 然后以某种方式将错误从类型 NSError!
向下转换为 AutoreleasingUnsafeMutablePointer<NSError?>
,那么有人可以解释这是怎么发生的吗?
谢谢!
AutoreleasingUnsafeMutablePointer 相当于 Objective-C 中的 NSError**,在方法中用作 inout 表达式。语法看起来很奇怪。
最可靠的方法是同时考虑这两个错误并定义第二个错误变量。由于 GET 是 NSURLRequest 的默认 HTTP 方法,因此不可变请求就足够了。
let url = "http://google.com?test=toto&test2=titi"
let request = NSURLRequest(URL: NSURL(string: url)!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
if error != nil {
// handle NSURLConnection error
} else {
var jsonError : NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary {
// process jsonResult
} else {
// couldn't load JSON, look at jsonError
}
}
})