Function Swift-如何在函数的参数中传递带有参数的函数?

Function Swift-如何在函数的参数中传递带有参数的函数?,function,parameters,swift,Function,Parameters,Swift,我知道标题听起来很复杂,但问题不是这样 我有这个Swift代码: class MyClass { let helloWorld: (check: Bool)->() init(helloWorld: (check: Bool)->()) { self.helloWorld = helloWorld } } let instanceOfMyClass = MyClass(helloWorld: (check: Bool) -> { }) 这给了我一个错误。

我知道标题听起来很复杂,但问题不是这样

我有这个Swift代码:

class MyClass {
let helloWorld: (check: Bool)->()

init(helloWorld: (check: Bool)->()) {
    self.helloWorld = helloWorld
    }
}

let instanceOfMyClass = MyClass(helloWorld: (check: Bool) -> {

})
这给了我一个错误。最后一条指令的正确语法是什么

谢谢

您可以使用:

let instanceOfMyClass = MyClass(helloWorld: { (check) in println(check) } )
但如果闭包是最后一个参数,则可以使用语法,其中闭包写在函数(在您的例子中为init)括号之外-这更易于阅读:

let instance = MyClass() { (check) in
    println(check)
}
还有其他定义闭包的快捷方式,例如:

let instance2 = MyClass() { println($0) }
但我建议你在官方的swift手册中阅读全文


注意:在我上面的代码中,用您的实际处理替换
println(…)
,谢谢!谢谢你的链接。我找不到,不客气。很高兴知道我的回答对你有帮助;-)当取出闭包后没有更多参数时,可以省略括号:
MyClass{println($0)}