1

Signature of a objective-c function i've used in the past is:

- (MMCall *)getLocalizedAppCategoriesWithOptions:(MMOptions *)options
                          success:(void (^)(NSArray *response))success
                          failure:(void (^)(NSError *error))failure;

I use to call it as -

[[whateverclassname new] getLocalizedAppCategoriesWithOptions:nil
                                                        success:^(NSArray *response) {
    }   failure:[^(NSError *error) {
}

It returns an NSArray of objects. These objects' parent class is NSObject and they have few properties. While experimenting with swift, i updated the bridging header file with the respective header file. Issues are -

  1. If I call it following way, i get compile error as "'[AnyObject]' is not identical to 'NSArray'"

    whateverclassname().getLocalizedAppCategoriesWithOptions(nil, success: {(response : NSArray!) in
        }, failure: {(error: NSError!)  in
    })
    
  2. If I call it following way, it compiles successfully but app crashes at runtime -

    whateverclassname().getLocalizedAppCategoriesWithOptions(nil, success: {(response : [AnyObject]!) in
        }, failure: {(error: NSError!)  in
    })
    

Please help me find out what's wrong with this?

pkamb
  • 33,281
  • 23
  • 160
  • 191
Avi
  • 2,196
  • 18
  • 18

1 Answers1

2

Found answer to both of my questions -

  1. Since NSArray is immutable and [AnyObject] is Array type in Swift that's mutable, compiler complains they are not identical. I changed NSArray! to Array! in Swift code. Other option is was to change Objective-C code to success:(void (^)(NSMutableArray *response))success (not preferred as it kills the purpose of using the bridge).

  2. Cast the Array to NSMuatableArray before calling the block in Objective-C

pkamb
  • 33,281
  • 23
  • 160
  • 191
Avi
  • 2,196
  • 18
  • 18