gpt4 book ai didi

ios - 没有参数的Swift中的返回函数类型

转载 作者:搜寻专家 更新时间:2023-11-01 06:08:42 26 4
gpt4 key购买 nike

我有一个没有任何参数的函数 displayForPhone 和 displayForPrinter。我想从另一个名为“display”的函数返回这些函数。我在 Swift Playground 中有以下代码,但它只是说有错误,但从未告诉我错误是什么:

func displayForPrinter() {
println("Displaying for printer")
}

func displayForPhone() {
println("Displaying for phone")
}

func display:(shouldDisplayForPhone :Bool) -> (void) -> (void) {

return shouldDisplayForPhone ? displayForPhone : displayForPrinter
}

最佳答案

把你的代码改成这样

func displayForPrinter() {
println("Displaying for printer")
}

func displayForPhone() {
println("Displaying for phone")
}

func display(shouldDisplayForPhone :Bool) -> (Void) -> (Void) {
return shouldDisplayForPhone ? displayForPhone : displayForPrinter
}
//test
let function = display(true)
function()

关于ios - 没有参数的Swift中的返回函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30471543/

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