I am trying to build an Objective-C block in Swift 2 to add it to NSArray , for example:
typealias CompletionBlock = () -> Void let aBlock:CompletionBlock = { print("Hello world!") } let nsArray = NSMutableArray() nsArray.addObject(aBlock) // Error
I know this will work just fine with a Swift array, but I need NSArray here for compatibility with existing Objective-C code. And if I use a fast array, the compiler will refuse to give it to NSArray because it will not be [AnyObject] (it will be [Any] ).
The problem is that quick closure is not the opposite of Objective-C blocks, which are objects behind the scene (they are instances of NSBlock , which is a subclass of NSObject )
So my question is: how to create an Objective-C block in swift? I tried using @convention (block) in typealias but it does not work.
source share