Swift 4: Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

Esqarrouth picture Esqarrouth · Jul 18, 2017 · Viewed 16.5k times · Source

Using XCode 9, Beta 3. Swift 4.

statsView.createButton("Button name") { [weak self] Void in
   //stuff stuff
   self?.doSomething()
}

I get hundreds of errors like this, how do I fix them?

Errors:

Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()'

Argument passed to call that takes no arguments

Answer

Zeynep Akkalyoncu Yilmaz picture Zeynep Akkalyoncu Yilmaz · Jul 18, 2017

It seems we don't use Void in in Swift 4 anymore. How I fixed them:

Delete the Void keyword:

statsView.createButton("Button name") { [weak self] in
   //stuff stuff
   self?.doSomething()
}

You have to use in or the compiler with complain with

Expected ',' separator

If there is no arguments then don't use Void in at all.

statsView.createButton("Button name") { //No "Void in" in here
   print("hi")
}