Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 18, 2009, 01:16 PM   #1
Darkroom
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
Toggling Switch Statement Expression?

ok, so i've got this lengthy Switch statement that i'd like to reuse with two different expressions. for both expression, the case constants are the same (they're just integers), but in order to refactor my code i can't seem to get this right, or know if it's even possible.

one expression will rely on the pressing of buttons with tags, while the other relies on the user defaults.

Code:
//first expression
switch ([sender tag])
	{
	case (1):	//Do Something
			break;
	case (2):	//Do Something
			break;
	...

//second expression
switch ([defaults integerForKey:kWhateverKey])
	{
	case (1):	//Do Something
			break;
	case (2):	//Do Something
			break;
	...
i've attempted setting up an if/else statement before the switch statement assigning values to [sender tag], but that doesn't seem to work.

Code:
if ([defaults intergerForKey:kWhateverKey] == 1)
        [sender tag] == 1;

switch ([sender tag])
	{
	case (1):	//Do NSUserDefaults Thing
			break;
i've also tried using an NSString to replace the expression, but switch statements don't like that very much, claiming the switch quantity is not an interger

Code:
NSString *switchString = @"[defaults integerForKey:kWhateverKey]";

switch (switchString)
	{
	case (1):	//Do NSUserDefaults Thing
			break;
finally, i've tried sending the switch statement the argument of the NSUserDefault, but that wasn't working either, and probably couldn't ever work since the defaults don't have tags.

Code:
-(void)awakeFromNib
{
[self switchMethod:[defaults integerForKey:kWhateverKey]];
}

-(void)switchMethod:(id)sender
{
switch ([sender tag])
	{
	case (1):	//Do Something
			break;
	case (2):	//Do Something
			break;
	...
}}
so, am i absolutly forced to have two identical, lengthy switch statements with the exception of the switch expression? is there no way to make this work?
Darkroom is offline   0 Reply With Quote
Old Jul 18, 2009, 01:28 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
switch will only work with an integer argument, so you can't use strings.

Also, you can't do this:

Code:
NSString *switchString = @"[defaults integerForKey:kWhateverKey]";
Anything inside the quotes gets made into the string so the call is never made. Instead do something like this:

Code:
NSString *switchString = [NSString stringWithFormat:@"%d", [defaults integerForKey:kWhateverKey]];
It's probably best to just make an intermediate int variable, and assign or convert whatever to that first, then use it in a single switch block.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jul 18, 2009, 01:52 PM   #3
Darkroom
Thread Starter
Guest
 
Join Date: Dec 2006
Location: Montréal, Canada
Quote:
Originally Posted by HiRez View Post
Instead do something like this:

Code:
NSString *switchString = [NSString stringWithFormat:@"%d", [defaults integerForKey:kWhateverKey]];
It's probably best to just make an intermediate int variable, and assign or convert whatever to that first, then use it in a single switch block.
the stringWithFormat didn't work, but the intermediate int var did. thanks a lot!
Darkroom is offline   0 Reply With Quote
Old Jul 18, 2009, 06:56 PM   #4
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by Darkroom View Post
the stringWithFormat didn't work, but the intermediate int var did. thanks a lot!
Right, the stringWithFormat: was just an example of how you can convert an integer into a string, you still can't ever use a string in a switch statement, as it will only accept an int. But just for future reference.

Anyway, glad you got it working.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Help with switch statement. cybrscot Mac Programming 28 Jan 31, 2011 07:41 PM
Switch Statement with Char SkippyThorson Mac Programming 11 Feb 20, 2010 03:34 PM
Need default value to appear when using a switch statement StevenHu iPhone/iPad Programming 4 Feb 5, 2010 01:24 PM
Why doesn't this switch statement work... Aquis Mac Programming 14 Apr 3, 2009 09:37 PM
Ringer/Vibrate Toggle Switch doesnt hold well. Switches back & forth in my pocket RR08EvoX iPhone 3 Apr 30, 2008 09:16 PM


All times are GMT -5. The time now is 08:13 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC