Swift - 带参数作为回调的匿名函数(语法)

时间:2014-06-16 17:36:59

标签: callback arguments swift

由Apple潜入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")
})

如何解决?

1 个答案:

答案 0 :(得分:26)

你的闭包语法错误

test("hello", {(name: String) in 
    println("callback")
})

test("hello", {
   println("callback: \($0)")
})

test("hello") {(name: String) in 
    println("callback")
}

test("hello") {
   println("callback: \($0)")
}