tags:

views:

4384

answers:

4

In Objective-C 2.0, is it possible to make a method where the argument is optional? That is, you can have a method call like this:

[aFraction print];

as well as this:

[aFraction print: someParameter];

in the same program.

Apple's Objective-C 2.0 Programming Language guide contrasts Obj-C with Python and seems to say this is not allowed. I'm still learning and I want to be sure. If it is possible, then what is the syntax, because my second code example does not work.

Update: OK, I just made two methods, both named "print".

header

-(void) print;
-(void) print: (BOOL) someSetting;

implementation

-(void) print {
    [self print:0];
}

-(void) print: (BOOL) someSetting {
    BOOL sv;
    sv = someSetting;

    if ( sv ) {
     NSLog(@"cool stuff turned on");
    }
    else {
     NSLog(@"cool stuff turned off");
    }
}

the relevant program lines

    ...
    printParamFlag = TRUE;

// no parameter
    [aCodeHolder print];

// single parameter
    [aCodeHolder print:printParamFlag];
    ...

I can't believe that worked. Is there any reason I shouldn't do this?

+16  A: 

You can declare multiple methods:

- (void)print;
- (void)printWithParameter:(id)parameter;
- (void)printWithParameter:(id)parameter andColor:(NSColor *)color;

In the implementation you can do this:

- (void)print {
    [self printWithParameter:nil];
}

- (void)printWithParameter:(id)parameter {
    [self printWithParameter:nil andColor:[NSColor blackColor]];
}

Edit:

Please do not use print and print: at the same time. First of all, it doesn't indicate what the parameter is and secondly no one (ab)uses Objective-C this way. Most frameworks have very clear method names, this is one thing that makes Objective-C so pain-free to program with. A normal developer doesn't expect this kind of methods.

There are better names, for example:

- (void)printUsingBold:(BOOL)bold;
- (void)printHavingAllThatCoolStuffTurnedOn:(BOOL)coolStuff;
Georg
when I pass nil as an argument, I get a warning: "passing argument 1 of 'print:' makes integer from pointer without a cast". Should I just ignore it?
willc2
Could you post the line where this error occurs? I'm not really sure what you're doing. This error should not happen.
Georg
you can use NIL if the parameter type is (id). In my code the parameter type was (BOOL). The solution was to change NIL to 0 since that's the type expected. I get it now. The multiple methods thing is great.
willc2
You should use NULL instead of 0. nil => no instance, Nil => no class, NULL => no C data
Georg
In your specific case use NO instead of 0. BOOLs shouldn't be 0 or 1, but NO and YES.
Georg
+6  A: 

The way you did it is the right way to do it in Objective-C. It's used extensively in Cocoa itself. For example, some of NSString's initializers:

– initWithFormat:  
– initWithFormat:arguments:  
– initWithFormat:locale:  
– initWithFormat:locale:arguments:

The reason it works is because the : is part of the method name, so as far as the compiler is concerned, print and print: are completely different messages that are no more closely connected than "print" and "sprint".

However, the particular names of the methods you gave aren't a very good case for this, because it's unclear from the name what the parameter is (or what "print" by itself means if the parameter is what the object prints). It would be better to have, say, printFalseMessage and printMessageWithFlag:.

Chuck
Heh, it was just code from a book exercise. I have got the religion of descriptiveVariableNamesPinkySwearScoutsHonor = TRUE; Now that I'm working with a proper IDE, long names are no big.
willc2
+2  A: 

Slightly related you can have optional arguments. Meaning you can call a method with any number of arguments if you like. But that is a feature from C (varargs).

An example, is the NSArray message:

+ (id)arrayWithObjects:(id)firstObj, ...

Example of usage:

NSArray *myArray;
NSDate *aDate = [NSDate distantFuture];
NSValue *aValue = [NSNumber numberWithInt:5];
NSString *aString = @"a string";

myArray = [NSArray arrayWithObjects:aDate, aValue, aString, nil];

This is straight from the Apple docs. You indicate the end of all your arguments with a nil.

Adam Smith
Using that to represent optional arguments rather than a variable-length list for one argument would be quite perverse.
Chuck
+2  A: 

Another option for multiple optional arguments is to pass them all in an NSDictionary:

- (void)someMethodWithOptions:(NSDictionary *)options;

then declare that the options dictionary may contain:

- key1, value must be a BlahObject
- key2, value must be a NonBlahObject

you can then allow any of the keys to be absent and even the dictionary itself could be nil.

Matt Gallagher
This is very common in Ruby. Creating a dictionary is a bit verbose in ObjC-Cocoa, though, so it's generally better to save this for large sets of related options rather than use it as a "shortcut" for argument passing — it would be more like a scenic route. You also lose type-checking.
Chuck
I think Apple uses it too for some methods, like in strings.
Georg