John Anthony
John Anthony

Reputation: 547

Calling Objective-C method with blocks from Swift

I have a method in Objective-C as follows:

- (void)myFunction:(void (^)(NSArray *data))successBlock error:(void (^)(NSError *error))errorBlock {
    //...//
    successBlock(someData);
}

I want to call it from Swift, but I can't understand the syntax.

Whatever I try it complains:

SomeClass.sharedInstance().myFunction(
    successBlock: {
        (data) in
        print(data)
    },
    error: {
        (error) in
        print(error)
    })

Cannot call value of non-function type

'(((([AnyObject]!) -> Void!, error: ((NSError!) -> Void)!) -> Void)!

Upvotes: 0

Views: 933

Answers (1)

vadian
vadian

Reputation: 285059

This Swift equivalent is

SomeClass.sharedInstance().myFunction({ data in
     print(data) 
   }) { error in 
     print(error) 
   }

If you need the parameter names successBlockand errorBlock you have to declare them on the ObjC side.

Upvotes: 1

Related Questions