繁体   English   中英

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

[英]Return Function Type in Swift with no parameters

我有一个函数displayForPhone和displayForPrinter,它没有任何参数。 我想从另一个称为“显示”的函数返回这些函数。 我在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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM