Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift中的返回函数类型,不带参数_Ios_Swift - Fatal编程技术网

Ios Swift中的返回函数类型,不带参数

Ios Swift中的返回函数类型,不带参数,ios,swift,Ios,Swift,我有一个功能displayForPhone和displayForPrinter,没有任何参数。我想从另一个名为“display”的函数返回这些函数。我在Swift Playerd中有以下代码,但它只是说有错误,但从未告诉我错误是什么: func displayForPrinter() { println("Displaying for printer") } func displayForPhone() { println("Displaying for phone") }

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

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()

将代码更改为此

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()

删除无关的
和void应为void删除无关的
和void应为void感谢解决了问题!出于某种原因,斯威夫特从未告诉我在函数名后面加“:”。我发现斯威夫特现在处于非常不成熟的状态。嗯,斯威夫特是一种新语言,它会变得更好。顺便说一句,如果它对你有帮助,请接受我的回答是的,我会在1分钟内接受。StackOverFlow restriction:(谢谢你解决了这个问题!出于某种原因,斯威夫特从未告诉我我正在放置):在函数名之后。我发现Swift现在处于非常不成熟的状态。嗯,Swift是一种新语言,它会变得更好。顺便说一句,如果它对您有帮助,请接受我的回答是的,我会在1分钟内接受。StackOverFlow限制:(