Compilation error in Swift 4 when passing parameters

I used a third-party library in Xcode 9 Beta 3. And I get the following error in the termination call, I can not fix this error:

DispatchQueue.main.asyncAfter(deadline: .now() + delay) { self.animationView?.alpha = 0 self.containerView.alpha = 1 completion?() // -> Error: Missing argument parameter #1 in call. } 

And having received the following warning in the completion function:

 func openAnimation(_ completion: ((Void) -> Void)?) { // -> Warning: When calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'? } 
+7
swift ios11 swift4 xcode9-beta
source share
1 answer

In Swift 4, tuples are handled more rigorously than ever.

This type of closure: (Void)->Void means a closure that

  • takes one argument whose type is Void
  • returns Void , which means no value

So try the following:

Pass a value of type Void to the closure. (An empty tuple () is the only instance of Void .)

 completion?(()) 

Or else:

Change the type of completion parameter.

 func openAnimation(_ completion: (() -> Void)?) { //... } 

Remember that the two types (Void)->Void and ()->Void are different even in Swift 3. Thus, the latter would be appropriate if you intend to represent the type of closure without arguments.

This change is part of SE-0029 Remove the implicit tuple character behavior from function applications , which is said to be implemented in Swift 3, but it seems that Swift 3 did not fully implement it.


Here I will show you a simplified verification code that you can verify on the playground.

 import Foundation //### Compiles in Swift 3, error and warning in Swift 4 class MyClass3 { func openAnimation(_ completion: ((Void) -> Void)?) { DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { completion?() } } } //### Compiles both in Swift 3 & 4 class MyClass4 { func openAnimation(_ completion: (() -> Void)?) { DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { completion?() } } } 
+16
source share

All Articles