Compile error in Swift 4 on parameter passing

后端 未结 1 1587
暗喜
暗喜 2020-12-03 17:34

I used 3rd party library in Xcode 9 Beta 3. And I am getting the following error in the completion call, I am not able to resolve this error:

DispatchQueue.m         


        
相关标签:
1条回答
  • 2020-12-03 18:34

    In Swift 4, tuples are treated more stricter than ever.

    This closure type: (Void)->Void means a closure which

    • takes a single argument, of which the type is Void
    • returns Void, meaning returns no value

    So, try any of the followings:

    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 the parameter completion.

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

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

    This change is part of SE-0029 Remove implicit tuple splat behavior from function applications which is said to be implemented in Swift 3, but it seems Swift 3 has not implemented it completely.


    Here, I show you a simplified checking code which you can check the difference 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?()
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题