简体   繁体   English

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

[英]Return Function Type in Swift with no parameters

I have a function displayForPhone and displayForPrinter which does not have any parameters. 我有一个函数displayForPhone和displayForPrinter,它没有任何参数。 I want to return those functions from another function called "display". 我想从另一个称为“显示”的函数返回这些函数。 I have the following code in Swift Playground but it just says there is an error but never tells me what the error is: 我在Swift Playground中有以下代码,但它只是说有一个错误,但从不告诉我什么是错误:

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

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

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

    return shouldDisplayForPhone ? displayForPhone : displayForPrinter
}

Chang your code to this 将您的代码更改为此

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