0

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)!

2
  • The code completion is supposed to suggest you the proper syntax Commented May 24, 2016 at 12:58
  • If I autocomplete the suggestion Xcode gives me it fails with the same error Commented May 24, 2016 at 13:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.