2

I have a closure defined like this,

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

Which is updated like this,

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

And using it like this,

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

Error:

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

But this is giving me above error and not sure what to do?

The same is working fine with Swift 3.x.

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Someone, please tell me the reason to downvote my question? This is completely different than the previously asked. – Hemang Jun 19 '17 at 07:11

1 Answers1

2

The reason its not working in Swift 4 is because of Distinguish between single-tuple and multiple-argument function types(SE-0110).

If you still want to work in a way you are doing in Swift 3 than you need to set the function type's argument list to enclosed with Double parentheses like this.

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

Now you all set to go

Printer.log.onLogCompletion = { (log) in
    //print(log)
    //print(log.0)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183