Nil cannot be assigned to type ()->()?

后端 未结 3 1765
花落未央
花落未央 2021-01-14 05:10

So this is a function from a class that lets you have a double tap and a single tap gesture. It\'s working fine in Swift 2.3, but after converting to Swift 3 it\'s throwing

相关标签:
3条回答
  • 2021-01-14 05:25

    From another topic:

    func someFunction(someParameter: someType, completionClosure: @escaping @autoclosure () -> ()? = nil)
    

    The use of @autoclosure allowed me to assign nil to the optional closure.

    The full details of how exactly @autoclosure helps escape me (why "my" closure is "not good" but the Swift_generated_one_that_simply_wraps_mine is?) . But, it helped me get rid of ... ...and call my function in either of two ways:

    someFunction(x, { ... }) 
    

    OR

    someFunction(x)
    
    0 讨论(0)
  • 2021-01-14 05:43

    Here is the way how i could overcome this. Here clang is not aware about its type. I gave a typealias and optional declaration,

    typealias appDelegateBlock = ()->()
    

    In your class, just declare it as optional,

    var block: appDelegateBlock?
    block = task
    
    0 讨论(0)
  • 2021-01-14 05:44

    Try with: var closure: (()->())? = task

    0 讨论(0)
提交回复
热议问题