簡體   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