1

Im trying to implement CometChat in my swift application. I managed to import the Objective-c framework successfully via a bridging header. But now I'm stuck trying to call Objective-C methods from swift.

This is the method from the interface i want to call:

- (void)loginWithURL:(NSString *)siteURL
            username:(NSString *)username
            password:(NSString *)password
             success:(void(^)(NSDictionary *response))success
             failure:(void(^)(NSError *error))failure;

And this is how the method is called from Objective-C:

 [cometChat loginWithURL:@"localhost/cometchat/" username:usernameTextField.text password:passwordTextField.text success:^(NSDictionary *response) {

                    NSLog(@"SDK log : Username/Password Login Success %@",response);

                    [self handleLogin];

                } failure:^(NSError *error) {

                    NSLog(@"SDK log : Username/Password Login Error%@",error);

                    [self handleLoginError:@[@0,error]];

                }];

So far i have this:

 cometChat.loginWithURL("localhost/cometchat/", username: EmailField.text, password: PasswordField.text){
            (success: [NSDictionary], failure:NSError) in {

            println("did i make it here?")

            }

    }

The problem is, it says that there is missing argument "success", but its a mystery to me how it can be an argument, when it clearly returns the response. I want to know how to put together this method call. I also used the objectivec2swift converter, but it wasn't any help. Also, i have no clue what the @ means before the @[@0,error]

I know its a beginners question, but i already wasted a whole day on this, since i couldn't find any tutorials on how to call such "complex" Obj-C methods from swift.

3 Answers 3

4

Try this :-

cometChat.loginWithURL("localhost/cometchat/", username: "abc", password: "123", success: { (response) -> Void in

        print("SDK log : Username/Password Login Success \(response)")


        }) { ( error) -> Void in

            print("SDK log : Username/Password Login Error \(error)")
    }
Sign up to request clarification or add additional context in comments.

Comments

1

When you look at the Objective-C signature, you see that the method takes two closures: success is a void function that takes a dictionary, and failure is a void function that takes an error.

In your Swift code you have only one closure: a void function that takes a dictionary and an error.

You either need to change the Objective-C method to take just one closure, or change the Swift code to provide two closures.

2 Comments

so something like (dont have access to the mac to test right now): cometChat.loginWithURL("localhost/cometchat/", username: EmailField.text, password: PasswordField.text){ (success: [NSDictionary]) in { println("did i make it here?") }, (failure:NSError) in{} }
Syntax is object.method (value1, param2: value2, param3: value3, param4: value4, param5: value5). That's not what you are writing. Read the Swift book. It seems you haven't learned the language but are just copying examples; that doesn't work. Again, read and understand the book.
1

When you call a function, and the last parameter is a block / closure, then you can write the last parameter after the function call in { }. That applies to the last block only.

Anyway, you are trying to pass a closure with two parameters success and failure. You need to pass two closures, one as the success parameter of your function, with a parameter response, and one either as the failure parameter of your function, or following the function, with a parameter error.

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.