How to write a method/message with multiple parameters?

40,423

Solution 1

You can write the declaration as such:

- (void) drawRoundedRect:(NSRect)aRect inView:(NSView *)aView withColor:(NSColor *)color fill:(BOOL)fill

The subsequent call (with 4 parameters) could look like:

[self drawRoundedRect:rect inView:self withColor:[NSColor greenColor] fill:YES];

where rect is a previously defined NSRect, self is the NSView the method is called from, an NSColor object obtained from a nested method call, and the constant boolean value YES.

Solution 2

In Objective-C, method names are properly called "selectors", and can be composed of one or more parts. If the method accepts one or more parameters, each part of the selector is of the form:

selectorFragmentName:(ParameterType)parameterName

For example, you will see method declarations like this one from NSColor:

+ (NSColor*) colorWithDeviceRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;

In this case, the method (selector) name is colorWithDeviceRed:green:blue:alpha: — the rest signifies the scope (- for instance method, + for class), return type (NSColor* here), and the type and name for each parameter.

CRITICAL! Unlike most other languages, you cannot overload methods in Objective-C — this means you can't have two methods with the same selector but different number of parameters and/or order of types. For example, you can't have these two methods:

- (id) initWithObjects:(NSArray*)anArray;
- (id) initWithObjects:(NSSet*)aSet;

Since the selector name for both is initWithObjects: Objective-C does not distinguish between the two. Selector names are translated into unique integers for extremely fast lookup, which is beneficial in the dynamic runtime, but a letdown for people that expect method overloading. The most common case for overloading in languages like Java is constructors, which is a non-issue in Objective-C because of the alloc/init pattern. For other methods, choosing unique names avoids the problem.

From a style standpoint, since the parameters are interspersed in the method selector, Objective-C programmers (and Xcode) will often align the parts of long selectors at the colon for readability, both for the declaration/definition:

+ (NSColor*) colorWithDeviceRed:(CGFloat)red
                          green:(CGFloat)green
                           blue:(CGFloat)blue
                          alpha:(CGFloat)alpha;

and invocation:

NSColor* myColor = [NSColor colorWithDeviceRed:0.5
                                         green:0.6
                                          blue:0.7
                                         alpha:0.9];

The whitespace is irrelevant to the compiler. If it makes it easier for you to read and understand, definitely use it.

Solution 3

Jeff accurately described what the methods look like. If you want to see how it would look as a C function, it would look something like:

void drawRoundedRect_inView_withColor_fill( MyObject* self, SEL _cmd, NSRect aRect, NSView* aView, NSColor* color, BOOL fill );

The parameter "names" all join together to form a single method name, and two hidden parameters, self and _cmd are added to the front.

Share:
40,423
seth-1
Author by

seth-1

Updated on July 24, 2020

Comments

  • seth-1
    seth-1 almost 4 years

    How do you write a method/message with multiple parameters?

    EDIT: Like multiple parameters for a single method/message, I mean.

  • seth-1
    seth-1 almost 15 years
    this is multiple methods, no?
  • Jeff Hellman
    Jeff Hellman almost 15 years
    Nope. The call is enclosed in a single set of [] so it must be a single method. It's a single method with 4 parameters.
  • wao813
    wao813 over 10 years
    The explanation of the object-c concept even with comparison with JAVA is really helpful for me to understand it. Great post!