Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Default Parameters for Obj-C Methods?

Default Parameters for Obj-C Methods?
Thread Tools
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status: Offline
Reply With Quote
Nov 8, 2005, 11:24 PM
 
I'm currently writing an Obj-C app. I've created a class that I use in several different places, and inside that class I have a method with no arguments. I realized that I need to expand that method to do something slightly different in a certain case.

In C++ I would create a default parameter so I could leave the rest of my code unchanged, but handle the special case with a variable.

(ie.
Code:
void myFunc(bool myVar=false); void myFunc(bool myVar) { if (myVar) { doSomething(); } }
However, I can't figure out if it is possible in Obj-C. The code below creates a compiler error. Any suggestions?

Code:
@interface myClass : NSObject { id myVars; } - (id) myFunc:(BOOL)myVar=NO;
(Last edited by smitty825; Nov 8, 2005 at 11:26 PM. (Reason:retitle for posting work-around))
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Nov 9, 2005, 08:35 AM
 
Nope. But you can do this:

- (id)myFunc:(BOOL)myVar {
// do stuff
}

- (id)myFunc {
return [self myFunc:NO];
}

Edit: stupid graphical smileys.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Nov 9, 2005, 03:41 PM
 
The idea of default values doesn't really mesh with Objective-C. Instead of having a method name and argument list, Objective-C encourages a more self-documenting style where you name the arguments as part of the method, like [object movePhilange:foo toPosition:bar].

As Wevah said, though, you can create a method that calls another method with assumed arguments. Lots of Cocoa classes do this.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Forum Regular
Join Date: Sep 2003
Location: San Diego
Status: Offline
Reply With Quote
Nov 9, 2005, 10:53 PM
 
Thanks for the responses. I now have another item on my Obj-C wish list I guess I'll just do a function that calls another function!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:41 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2