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 online now   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 online now   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

Similar Threads
thread Thread Starter Forum Replies Last Post
Hide OSX System Preferences in Applescript for toggling ignore trackpad in Mavericks? mactp Mac Programming 9 Mar 16, 2014 06:03 PM
Can someone tell me if toggling LTE extends battery life? Rainforest iPhone 5 Oct 20, 2012 06:44 PM
Toggling 4G dylanlive iOS 6 1 Sep 15, 2012 05:04 PM
Toggling Airport causes Computer to Freeze! applechew Mac OS X Server, Xserve, and Networking 0 Aug 20, 2012 10:42 PM
Alternatives to a switch statement? GorillaPaws Mac Programming 3 Jun 14, 2012 12:18 PM

Forum Jump

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

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

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