gpt4 book ai didi

callback - Swift - 尾随闭包语法

转载 作者:IT王子 更新时间:2023-10-29 05:20:52 27 4
gpt4 key购买 nike

我正在研究 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")
})

如何解决?

最佳答案

你的闭包语法错误

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

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

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

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

关于callback - Swift - 尾随闭包语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com