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.
2 Answers 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.)
Try believing what the error message is telling you - remove the parameter names:
sender.sendRegistrationEmail(username, email)
7 Comments
em
would probably stayusername
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.
-(bool)sendRegistrationEmail:( NSString *)un :( NSString *)em
, just do not do such thing. ever. follow this guidance.