Method with multiple input parameters

后端 未结 6 1868
萌比男神i
萌比男神i 2021-02-01 02:11

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

相关标签:
6条回答
  • 2021-02-01 02:26

    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.

    0 讨论(0)
  • 2021-02-01 02:29

    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.

    0 讨论(0)
  • 2021-02-01 02:31

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

    0 讨论(0)
  • 2021-02-01 02:36

    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

    0 讨论(0)
  • 2021-02-01 02:41

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

    - (void)first:(NSString *)fname second:(NSString *)mname third:(NSString *)lname;
    
    0 讨论(0)
  • 2021-02-01 02:44
    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.

    0 讨论(0)
提交回复
热议问题