前端之家收集整理的这篇文章主要介绍了
Swift – 具有参数的匿名函数作为回调(语法),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在由苹果潜入
Swift lang,并且有一些
调用简单匿名
函数的参数作为另一个
函数的参数,例如:
- func test(txt: String,resolve: (name: String) -> Void) {
- resolve(name: "Dodo")
- }
-
- // Errors here complaining on resolve param
- test("hello",(name: String) {
- println("callback")
- })
如何解决?
你有
错误的
关闭语法
- test("hello",{(name: String) in
- println("callback")
- })
要么
- test("hello",{
- println("callback: \($0)")
- })
要么
- test("hello") {(name: String) in
- println("callback")
- }
要么
- test("hello") {
- println("callback: \($0)")
- }