Toggling Switch Statement Expression?

Discussion in 'Mac Programming' started by Darkroom, Jul 18, 2009.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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?
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    the stringWithFormat didn't work, but the intermediate int var did. thanks a lot! :)
     
  4. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    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.
     

Share This Page