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
  • 19,824
  • 17
  • 99
  • 186
mba12
  • 2,702
  • 6
  • 37
  • 56
  • 4
    such method declaration is highly improper: `-(bool)sendRegistrationEmail:( NSString *)un :( NSString *)em`, just do not do such thing. ever. follow [this guidance](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html). – holex Jun 08 '15 at 16:14
  • 2
    "Extra argument 'un' in call" It's not misleading. What part of it don't you understand? – matt Jun 08 '15 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. – mba12 Jun 08 '15 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. – matt Jun 08 '15 at 16:26

2 Answers2

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

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thank you , that did the trick. Adding the parameters in the header file was my serious oversight. Much appreciated. – mba12 Jun 08 '15 at 18:21
  • 1
    Leaving off the external parameter name from parameters in Objective-C is technically legal, but seriously frowned upon. – Duncan C Jun 08 '15 at 18:38
1

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

sender.sendRegistrationEmail(username, email)
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • `em` would probably stay – Ben Kane Jun 08 '15 at 16:18
  • 1
    @BenKane Not the way he's written the Objective-C method declaration it wouldn't. – matt Jun 08 '15 at 16:19
  • 1
    Thanks for the feedback. I had also tried sender.sendRegistrationEmail(username, email) and receive "Extra argument in call" as a build error. – mba12 Jun 08 '15 at 16:20
  • Ah yes you're right I don't know what I was thinking – Ben Kane Jun 08 '15 at 16:20
  • @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. – matt Jun 08 '15 at 16:25
  • @matt Thanks for the suggestion. sender.sendRegistrationEmail("test", "test") Did not compile so clearly something else is amiss. – mba12 Jun 08 '15 at 16:59
  • @mba12 I copied your Objective-C code, as shown in your question, directly and pasted it right into my app. So my declaration is _exactly_ the same as what you have shown in your question. If you changed your code after you pasted into the question, that's beyond my control (and beyond my ability to see). I can only go on what you actually show me. Let me know if you need me to send you a test project that proves that it works. – matt Jun 08 '15 at 17:30