0

I'm trying to call an Objective C method with multiple parameters from Swift. I followed the excellent set up instructions here: How do I call Objective-C code from Swift?

My .h header file:

NSMutableData *_responseData;
@interface RegistrationEmailSender : NSObject
- (bool) sendRegistrationEmail;
@end

My function/method declaration:

- (bool)sendRegistrationEmail:( NSString *) un 
 :( NSString *) em
{
 // send email
}

And lastly the call from a Swift class:

// Send User a Validation Email
var sender: RegistrationEmailSender = RegistrationEmailSender()
sender.sendRegistrationEmail(un: username as NSString, em: email as NSString)
 

I receive this error from XCODE:

Extra argument 'un' in call

I've read around and it seems the "extra argument" error message is misleading and it frequently has to do with type mismatches and other similar causes though I've gone out of my way to ensure the types match. I'm new to Swift and Objective C.

halfer
20.1k19 gold badges110 silver badges207 bronze badges
asked Jun 8, 2015 at 16:12
4
  • 4
    such method declaration is highly improper: -(bool)sendRegistrationEmail:( NSString *)un :( NSString *)em, just do not do such thing. ever. follow this guidance. Commented Jun 8, 2015 at 16:14
  • 2
    "Extra argument 'un' in call" It's not misleading. What part of it don't you understand? Commented Jun 8, 2015 at 16:16
  • I am trying to pass two string arguments, username as the first and email as the second. If the compiler is assuming only one argument for some reason why not complain about "em" ? As a newbie to Swift and Obj C just trying to find the correct syntax. Thanks. Commented Jun 8, 2015 at 16:23
  • It is not "assuming only one argument". You have declared two parameters, we need to pass two arguments. And you have declared neither of them with external parameter names so we need to have zero external parameter names. That seems a pretty easy idea to grasp. Commented Jun 8, 2015 at 16:26

2 Answers 2

2

There are a number of problems with your code.

- (bool)sendRegistrationEmail:( NSString *) un 
 :( NSString *) em

Should be

- (BOOL) sendRegistrationEmail: (NSString *) un
 em: (NSString *) em

The .h file needs exactly the same definition as the .m file. You can't skip the parameters.

Then when you call it it from Swift it should look like this:

sender.sendRegistrationEmail(username, em: email)

(You shouldn't need the cast to NSString, since Swift casts back and forth between types like String and NSString automatically.)

answered Jun 8, 2015 at 17:15
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you , that did the trick. Adding the parameters in the header file was my serious oversight. Much appreciated.
Leaving off the external parameter name from parameters in Objective-C is technically legal, but seriously frowned upon.
1

Try believing what the error message is telling you - remove the parameter names:

sender.sendRegistrationEmail(username, email)
answered Jun 8, 2015 at 16:17

7 Comments

em would probably stay
@BenKane Not the way he's written the Objective-C method declaration it wouldn't.
Thanks for the feedback. I had also tried sender.sendRegistrationEmail(username, email) and receive "Extra argument in call" as a build error.
Ah yes you're right I don't know what I was thinking
@mba12 Well I tried it and it works fine. If it doesn't, there is something wrong with your username or your email. Try sendRegistrationEmail("howdy", "ho") - it won't run properly, perhaps, but it compiles, which is all we are after at this point.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.