繁体   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