我正在学习苹果的Swift编程语言,使用尾随闭包语法时遇到了一些问题,例如: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)") }
resolve(name: name)
会抛出一个错误use of unresolved identifier 'name'
。如何在回调调用中传递参数? - Kosmetika