2015-05-27 45 views
0

我有一个没有任何参数的displayForPhone和displayForPrinter函数。我想从另一个称为“display”的函数返回这些函数。我在斯威夫特游乐场下面的代码,但它只是说,有一个错误,但从来不告诉我什么错误是:在没有参数的Swift中返回函数类型

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

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

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

    return shouldDisplayForPhone ? displayForPhone : displayForPrinter 
} 
+1

去除多余的':'和void应该是void – Ian

回答

5

张你的代码,这

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

谢谢你解决了这个问题!出于某种原因,Swift从来没有告诉我,我在函数名后面加上“:”。我发现Swift现在处于非常不成熟的状态。 –

+0

恩,swift是一种新的语言,它会变得更好。BTY,如果它可以帮助你,请接受我的回答 – Leo

+0

是的,我会在1分钟内接受。 StackOverFlow限制:( –

相关问题