简体   繁体   English

使用闭包作为函数参数时,快速分隔参数不带逗号

[英]Swift seperate parameter without a comma when using closures as a function parameter

In Swift 1.1 with Xcode 6.2, I'd like to understand what is going on here: 在带有Xcode 6.2的Swift 1.1中,我想了解这里发生了什么:

ExampleInfo(title: item.title) { self.getItemById(item.uniqueId) }

and why is this the same as: 为何与以下内容相同:

ExampleInfo(title: item.title, { self.getItemById(item.uniqueId) })

This is what i'm constructing: 这是我正在构造:

class ExampleInfo : NSObject {

    let title : NSString
    var exampleFunc: () -> (UIViewController)

    init(title: NSString, example exampleFunc:() -> (UIViewController)) {
        self.exampleFunc = exampleFunc
        self.title = title
    }
}

Ideally I want to read a part of the documentation that explains why this behavior is allowed and what it's called. 理想情况下,我想阅读文档的一部分,该文档解释了为什么允许这种行为以及其被称为什么。 (or am I missing something?). (或者我错过了什么?)。

Is this allowed only for closures? 仅允许关闭吗? (I can see that I am allowed to omit many things with closures, such as brackets and return statements). (我可以看到我可以省略很多带有闭包的内容,例如方括号和return语句)。

tia tia

that is called a trailing closure . 这就是所谓的尾随闭包 From the official 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. 尾随闭包是一个闭包表达式,它写在其支持的函数调用括号的外部(和之后)。

Read more in the official documentation here - https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html 在此处的官方文档中了解更多信息-https: //developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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