3

I have an Objective-C wrapper (ObjCWrapper.h and ObjCWrapper.m) with prototype

+ (void) login:(NSString *)username andPassword:(NSString *)password andErrorBlock:(SuccessBlock)errorBlock andSuccessBlock:(SuccessBlock)successBlock;

With typedef

typedef void (^SuccessBlock)(NSString *);

and implementation

+ (void)login:(NSString *)username andPassword:(NSString *)password andErrorBlock:(SuccessBlock)errorBlock andSuccessBlock:(SuccessBlock)successBlock
{
    // do stuff like
    successBlock(@"test");
}

From my swift view controller (ViewController.swift), I call the login function:

ObjCWrapper.login("abc", andPassword: "abc",
        andErrorBlock:
        {
            (error:String) -> Void in
            println();
        },
        andSuccessBlock:
        {
            (map:String) -> Void in
            println();
        }
    )

But I get error:

Cannot invoke 'login' with an argument list of type '(String, andPassword: String, andErrorBlock:(String)->void, andSuccessBlock:(String)->void)'

Searching in google says that I am passing some invalid types in the arguments, but I can't find anything wrong in the code. Removing the blocks from the function makes the code work, so I guess it is something related on the way of calling a block function.

Thanks for the help!

2

3 Answers 3

1

It might be worth adding nullability specifiers to your completion block:

typedef void (^SuccessBlock)( NSString * _Nonnull );

And to the method itself:

+ (void) login:(nonnull NSString *)username andPassword:(nonnull NSString *)password andErrorBlock:(nullable SuccessBlock)errorBlock andSuccessBlock:(nullable SuccessBlock)successBlock;

Then you should be able to call your method in Swift:

        ObjCWrapper.login("login", andPassword: "pass", andErrorBlock: { (error:String) -> Void in
        //error handling
        }) { (map:String) -> Void in
            //other stuff
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Adam, thanks a lot for the help. But I still get the same error
0

Obj-C NSString != Swift String With other word, you pass a String where a NSString is expected. Casting it down should solve that.

Comments

0

This is what I ended up doing

let username = "username" //usernameField.text
let password = "password" //passwordField.text

        ObjCWrapper.login(username, andPassword: password,
            andErrorBlock:
            {
                (map) -> Void in
                // stuff
            })
            {
                (map) -> Void in
                // stuff
            }

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.