40

I understand how to create my own methods that accept input parameters in objective-c but I have never actually created a method with more than one input parameter!

From methods I have used with multiple input parameters each has a name along the lines of

first:second:third:

and look like

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;

my question is when creating your own method with multiple input parameters do you have to create a name like first:second:third or can you just have something like C++ where you have the one name followed by a list of input parameter types followed by the parameter names... if I remember correctly.

fullName:(NSString, NSString, NSString) fname, mname, lname;
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
C.Johns
  • 10,185
  • 20
  • 102
  • 156

6 Answers6

57

No. A method must have the format as you described:

- (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;
Sam B
  • 27,273
  • 15
  • 84
  • 121
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • 2
    Incorrect, one does not have to do this, please see below answers! @PengOne – ericn Mar 03 '14 at 17:07
  • 1
    @fuzzybee Actually, this is completely correct. You may have an empty string for a name, e.g. "" instead of "second", but the colons are necessary after each name. – PengOne Mar 04 '14 at 20:19
  • Ok, understood now. However, one may get confused without you making yourself clearer. It's probably the reason why your answer were not accepted, just my 2 cents – ericn Mar 05 '14 at 01:21
  • how can I call that method in view did load? and assign values – Xcodian Solangi Apr 16 '18 at 06:12
26

You have to have the parameters interleaved with the method signature. It's ok because xcode has code completion and it can give you nice descriptive names about what your method is doing and what it requires.

e.g.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

In the example above without even looking at the API for UIViewController you can get a pretty good understanding of how this method works and what it's params are. It is good practice to name your methods well to describe what they do (it can remove the need for most commenting if done well).

You may well of course see a method written like this

- (void)myMethodThatAcceptsARectangle:(float)x :(float)y :(float)w :(float)h;

But this will not be very clear in use as to what the parameters relate to:

[self myMethodThatAcceptsARectangle:1.0f :1.0f :1.0f :1.0f];

So you should avoid this (I added it incase you ever see this and wonder what's happening).

Paul.s
  • 38,494
  • 5
  • 70
  • 88
20
fullName:(NSString, NSString, NSString) fname, mname, lname;

Yes, you can do something like that. It'd look like this instead:

-(void)fullName:(NSString*)fname :(NSString*)mname :(NSString*)lname

and you'd call it like this:

[foo fullName:first :middle :last];

That largely defeats the point of Objective-C's method names, though, and the main reason to do something like that is to register your dislike of the normal Objective-C convention, or perhaps to get yourself kicked off whatever project you're working on.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Caleb
  • 124,013
  • 19
  • 183
  • 272
4

Another option could be variadic parameters. They're used to provide a variable amount of parameters, even though you wouldn't have a name on each one of them. e.g.

[NSString stringWithFormat:@"My name is %@ %@", @"John", @"Doe"];

It would be something like this:

- (void)names:(NSString *)names, ...;

Implementation, additional info

Iree
  • 358
  • 3
  • 13
1

Here's a simple example for Method with parameters.

- (void)methodName:(NSString *)parameterOne methodNameContinues:(NSString *)parameterTwo;

For Example,

-(void)showAlertMsg:(NSString *)message withTitle:(NSString *)title;

Here you can see, we've a prefix "withTitle" for the second parameter. We've to proceed the same for the other parameters too.

Balaji Ramakrishnan
  • 1,909
  • 11
  • 22
-6

I can think of a perfectly good reason to use NSDictionary to pass arguments. I also believe it answers the question.

You can place all of the items in an NSDictionary then unpack them. This maybe useful if you have say a persitanceStore NSObject that you want to send a list of parameters to.

Mixstah
  • 411
  • 1
  • 7
  • 22