简体   繁体   中英

Xcode closure auto completion issue

The following is working code built against iOS 8.4 using Xcode 6.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
    }
})

If I double click the closure section of the method signature as Xcode auto completes it I end up in this state:

在此处输入图片说明

Xcode has not put }) at the end of the closure and also added -> Void in .

Is this a bug in Xcode 6.4 or are there two alternate syntaxes for closures?

When would I need completionHandler : { arg, arg arg in versus completionHandler : {(arg,arg,arg) -> Void in //code })

The way that Xcode autocomplete your instruction is in the "trailing closures style."

From the Apple documentation:

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

If you want to know more about the trailing closure, please report to the doc here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html (cf section Trailing Closures)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM