Optional arguments in Objective-C 2.0?

Objective C

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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;

Solution 2 - Objective C

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:.

Solution 3 - Objective C

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.

Solution 4 - Objective C

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.

Solution 5 - Objective C

To avoid the Parse issue: 'aVariable' used as the name of the previous parameter rather than as part of the selector you get from the compiler as a warning you should do:

- (void)printWithParameter:(BOOL)sv color:(NSColor *)color{
   // your cool code goes here
   if ( sv ) {
	  NSLog(@"cool stuff turned on");
   }
   else {
	  NSLog(@"cool stuff turned off");
   }
}

and you could call the method, such:

[self printWithParameter:NO color:[UIColor someColor]] // NO instead of 0

or

[self printWithParameter:YES color:[UIColor someColor]] // YES instead of 1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwillc2View Question on Stackoverflow
Solution 1 - Objective CGeorg SchöllyView Answer on Stackoverflow
Solution 2 - Objective CChuckView Answer on Stackoverflow
Solution 3 - Objective CMatt GallagherView Answer on Stackoverflow
Solution 4 - Objective CErik EngheimView Answer on Stackoverflow
Solution 5 - Objective CJohn WilundView Answer on Stackoverflow