无法将类型“(_) -> ()”的值分配给类型“((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)
    }

错误:

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

但这给了我上述错误,我不确定该怎么办?

同样适用于 Swift 3.x。

它在 Swift 4 中不起作用的原因是 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)
}