How to determine the default argument value for a method in ios?

Possible duplicate:
Objective-C Default Argument Value

I could not find a way to define the default argument for the function in ios, as would normally be done in C ++. Is this possible in iOS? If there is no recommended job?

+7
source share
2 answers

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"; // Use string } 

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.

+28
source

See the answers here: Objective-C Default Argument Value

What you need to do is define a method that takes all the arguments, and then write convenience methods that take fewer arguments, then rotate and call the longer method with the default values ​​you want

+1
source

All Articles