Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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 ([COLOR="DarkOrange"][sender tag][/COLOR])
	{
	case (1):	[COLOR="Green"]//Do Something[/COLOR]
			break;
	case (2):	[COLOR="green"]//Do Something[/COLOR]
			break;
	...

//second expression
switch ([COLOR="DarkOrange"][defaults integerForKey:kWhateverKey][/COLOR])
	{
	case (1):	[COLOR="Green"]//Do Something[/COLOR]
			break;
	case (2):	[COLOR="green"]//Do Something[/COLOR]
			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 ([COLOR="DarkOrange"][sender tag][/COLOR])
	{
	case (1):	[COLOR="Green"]//Do NSUserDefaults Thing[/COLOR]
			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 ([COLOR="DarkOrange"]switchString[/COLOR])
	{
	case (1):	[COLOR="Green"]//Do NSUserDefaults Thing[/COLOR]
			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 ([COLOR="DarkOrange"][sender tag][/COLOR])
	{
	case (1):	[COLOR="Green"]//Do Something[/COLOR]
			break;
	case (2):	[COLOR="green"]//Do Something[/COLOR]
			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?
 
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.
 
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! :)
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.