7 Replies
      Latest reply on Jan 9, 2017 3:58 PM by eskimo
      Dogstarsuper Level 1 Level 1 (0 points)


        I created a swift class like this:


        class SwiftClass
            override init()
            static func Test1(closure:()->Void){
            static func Test2(array:[()->Void]){
                for closure in array {


        Both 2 methods wok fine from swift file.




        The function Test1 work fine from objective-c file too.

        [SwiftClass Test1WithClosure:^{
                NSLog(@"SwiftClass Test1") ;
            }] ;


        but Test2 can't be used from objective-c file,the automatically generated -Swift.h file didn't declare it at all.

        @interface SwiftClass : NSObject
        - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
        + (void)Test1WithClosure:(void (^ _Nonnull)(void))closure;


        Is it possible to call Test2 from objective-c file?

        • Re: Passing an array of closures to a swift method form objectiv-c file
          QuinceyMorris Level 8 Level 8 (6,050 points)

          If you explicitly declare the array-based method as "@objc", you'll get the following compilation error message: "method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C".


          Since you don't get this error with an array of (say) Int, and with a non-array closure parameter, this suggests the compiler could make an @objc-compatible method, but chooses not to try. Or, there may be a technical reason why this array/closure combination can't be represented in Obj-C.


          As a workaround, you could change the Test2 parameter type to [AnyObject], or specify the parameter type as NSArray,  and cast individual elements as the closure type.


          Either way, it's probably worth a bug report, in case this is something that is really supposed to work.