This is not possible in Objective-C. One option is to do something similar to get the same functionality:
- (void)someMethodThatTakesAString:(NSString *)string { if (string == nil) string = @"Default Value";
If you are looking for the option to omit the argument altogether, the typical way that Objective-C executes is to have several methods with gradually more specific sets of arguments:
- (void)doSomethingWithOptionA:(BOOL)optionA andOptionB:(int)optionB { // Do something using options to control exactly what done } - (void)doSomethingWithOptionA:(BOOL)optionA { [self doSomethingWithOptionA:optionA andOptionB:42]; // Where 42 is the default for optionB } - (void)doSomething; { [self doSomethingWithOptionA:NO]; // NO is the default for optionA }
Then, if you want to do something with the default arguments, you can simply call -doSomething . If you want to set option A, but don't care about option B, you can call -doSomethingWithOptionA: etc.
Finally, it is worth noting that you can use C ++ for writing for iOS, and you can also mix Objective-C and C ++ with Objective-C ++. All that has been said, always think carefully about how best to do something in the environment that you use. Do not try to force idioms and C ++ templates on Objective-C and Cocoa. They are different, and they should be like that. It is often very easy to find a C ++ programmer who has not given up on C ++ conventions when moving on to writing Objective-C code, and this is usually not very good.
Andrew Madsen
source share