PDA

View Full Version : Toggling Switch Statement Expression?




Darkroom
Jul 18, 2009, 01:16 PM
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.


//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.


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


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.


-(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?



HiRez
Jul 18, 2009, 01:28 PM
switch will only work with an integer argument, so you can't use strings.

Also, you can't do this:

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:

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.

Darkroom
Jul 18, 2009, 01:52 PM
Instead do something like this:

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! :)

HiRez
Jul 18, 2009, 06:56 PM
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.