views:

293

answers:

3

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

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

+3  A: 

You can write the declaration as such:

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

The subsequent call could look like:

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

where recta is a previously defined NSRect, self is the NSView the function is called from, the NSColor is created on the spot and the BOOL yes is passed through

Jeff Hellman
this is multiple methods, no?
Devoted
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.
Jeff Hellman
+1  A: 

Jeff accurately described what the methods look like. If you want to think 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.

Peter N Lewis
A: 

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.

Quinn Taylor