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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This isn’t possible in Objective-C. One option is to do something like this to get the same functionality:
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:
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.