Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a default argument value to a method in ios? [duplicate]

Possible Duplicate:
Objective-C Default Argument Value

I couldn't find a way to define a default argument to a function in ios like you would normally do in C++. Is it possible in iOS? If not is there a work around recommended?

like image 304
tiguero Avatar asked Mar 05 '12 22:03

tiguero


People also ask

What are the default arguments for __ Init__ method?

How Does __init__() Method Work? The python __init__ method is declared within a class and is used to initialize the attributes of an object as soon as the object is formed. While giving the definition for an __init__(self) method, a default parameter, named 'self' is always passed in its argument.

Is it possible to give a default value to a function parameter Swift?

You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter's type. If a default value is defined, you can omit that parameter when calling the function.

Can we specify default value for template arguments?

Like function default arguments, templates can also have default arguments. For example, in the following program, the second parameter U has the default value as char.

How can we pass arguments to functions by default?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.


2 Answers

This isn't possible in Objective-C. One option is to do something like this to get the same functionality:

- (void)someMethodThatTakesAString:(NSString *)string
{
    if (string == nil) string = @"Default Value";

    // Use string
}

If you're looking for the ability to omit an argument altogether, the typical way that's done in Objective-C is to have multiple methods with progressively more specific argument sets:

- (void)doSomethingWithOptionA:(BOOL)optionA andOptionB:(int)optionB
{
   // Do something using options to control exactly what's 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 doSomething with just the default arguments, you can just call -doSomething. If you want to set optionA, but don't care about optionB, you can call -doSomethingWithOptionA: etc.

Finally, it's worth noting that you can use C++ to write for iOS, and you can also mix Objective-C and C++ using Objective-C++. All that said, always think carefully about how best to do things within the environment you're using. Don't try to force C++ idioms and patterns onto Objective-C and Cocoa. They're different and they're meant to be that way. It's often very easy to spot a C++ programmer who hasn't given up C++ conventions when transitioning to writing Objective-C code, and that's usually not a good thing.

like image 159
Andrew Madsen Avatar answered Sep 24 '22 02:09

Andrew Madsen


Take a look at answers here: Objective-C Default Argument Value

What you should do is define a method that takes all the arguments and then write convenience methods that take fewer arguments, then turn around and call the longer method with the default values you want

like image 30
JoshRagem Avatar answered Sep 23 '22 02:09

JoshRagem