以下是使用 Xcode 6.4 针对 iOS 8.4 构建的工作代码
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: { response, data, error in
if error != nil {
println("there be an error")
} else {
let image = UIImage(data:data)
self.webimage.image = image
}
})
如果我在 Xcode 自动完成时双击方法签名的关闭部分,我最终会处于这种状态:
Xcode 没有将 })
放在闭包末尾,并且还添加了 -> Void in
。
这是 Xcode 6.4 中的错误还是有两种闭包替代语法?
什么时候需要 completionHandler : { arg, arg arg in
与 completionHandler : {(arg,arg,arg) -> Void in//code })
最佳答案
Xcode 自动完成指令的方式是“尾随闭包样式”。
摘自苹果文档:
If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is a closure expression that is written outside of (and after) the parentheses of the function call it supports
如果您想了解有关尾随闭包的更多信息,请向此处的文档报告:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html (参见“尾随闭包”部分)
关于ios - Xcode 关闭自动完成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31511097/