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