ios - Xcode 关闭自动完成问题

标签 ios xcode swift xcode6

以下是使用 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 自动完成时双击方法签名的关闭部分,我最终会处于这种状态:

enter image description here

Xcode 没有将 }) 放在闭包末尾,并且还添加了 -> Void in

这是 Xcode 6.4 中的错误还是有两种闭包替代语法?

什么时候需要 completionHandler : { arg, arg arg incompletionHandler : {(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/

相关文章:

ios - NSDateFormatter 本地日期格式问题

ios - 为什么在 IOS 中单击按钮时 View 不显示?

xcode - 在 map 上选择一个位置会给出错误的坐标

ios - UITextField 添加到 UIView 时不响应触摸事件

swift - 如何在 Swift 中关闭没有按钮或交互的 UIAlert?

ios - 在 swift 中覆盖 fileprivate 扩展中的打开 var 时发出警告

c++ - 如何在 iOS 上使用 caffemodel 和 OpenCV?

ios - 如何同时使用带有可选和扩展的@objc 协议(protocol)?

objective-c - 方法中只有最后一行执行(Objective-C)

ios - 无法使用 SwiftUI Datepicker 重叠文本