john doe
john doe

Reputation: 9660

Return Function Type in Swift with no parameters

I have a function displayForPhone and displayForPrinter which does not have any parameters. 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:

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

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

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

    return shouldDisplayForPhone ? displayForPhone : displayForPrinter
}

Upvotes: 1

Views: 447

Answers (1)

Leo
Leo

Reputation: 24714

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

Upvotes: 5

Related Questions