Method with multiple input parameters

后端 未结 6 1877
萌比男神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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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).

提交回复
热议问题