Re: Weird calculation
Re: Weird calculation
- Subject: Re: Weird calculation
- From: Charles Srstka <email@hidden>
- Date: Thu, 24 Jan 2002 11:56:24 -0600
On Thursday, January 24, 2002, at 08:49 AM, Erik M. Buck wrote:
This is close to the worst possible way to solve this problem. If you
find
yourself using switch stsements in Objective-C, you are almost certainly
doing something wrong. Your code is very fragile. If you change the
order
of elements in the pop-up, your code will be wrong. If you add an item
to
the pop-up your code will be wrong.
I suggest that you set a different action for each item in the popup and
perform the correct calculation in each action.
You should never use switch statements? What's the correct way to do
something like this, then:
int answer = NSRunAlertPanel(@"header",@"question",@"button 1",@"button
2",@"button 3");
switch(answer)
{
case NSAlertDefaultReturn: // user pressed button 1
[self doWhateverTheDefaultButtonIsSupposedToDo];
break;
case NSAlertAlternateReturn: // user pressed button 2
[self doWhateverTheAlternateButtonIsSupposedToDo];
break;
case NSAlertOtherReturn: // user pressed button 3
[self doWhateverTheOtherButtonIsSupposedToDo];
break;
}
Also, how should one do this:
switch([[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"])
{
enum someSetOfPreferences {
doOneThingByDefault;
doAnotherThingByDefault;
askUserEachTime;
}
case doOneThingByDefault:
// do the thing
break;
case doAnotherThingByDefault:
// do things according to this setting
break;
case askUserEachTime:
// ask the user and do what he/she says
break;
}