簡體   English   中英

無法將類型為((_)->()''的值分配為類型為(((String,String,String,Int)->())?

[英]Cannot assign value of type '(_) -> ()' to type '((String, String, String, Int) -> ())?'

我有一個這樣定義的閉包,

public var onLogCompletion:((_ printLog:String,_ fileName:String,_ functionName:String,_ lineNumber:Int) -> ())? = nil

這樣更新,

fileprivate func printerCompletion(printLog:String, fileName:String, functionName: String, lineNumber:Int) -> Void {
    if onLogCompletion != nil {
        onLogCompletion!(printLog, getFileName(name: fileName), functionName, lineNumber)
    }
}

像這樣使用

    Printer.log.onLogCompletion = { (log) in
        //print(log)
        //print(log.0)
    }

錯誤:

無法將類型為((_)->()''的值分配為類型為(((String,String,String,Int)->())?

但這給了我上面的錯誤,不知道該怎么辦?

使用Swift 3.x同樣可以正常工作。

它在Swift 4中不起作用的原因是由於Distinguish between single-tuple and multiple-argument function types(SE-0110)進行Distinguish between single-tuple and multiple-argument function types(SE-0110)

如果您仍然想在Swift 3中進行某種工作,則需要將函數類型的參數列表設置為用Double parentheses括起來,如下所示。

public var onLogCompletion:(((String,String,String,Int)) -> ())? = nil

現在你們都准備去

Printer.log.onLogCompletion = { (log) in
    //print(log)
    //print(log.0)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM