Question

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

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top