• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Weird calculation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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;
}


  • Follow-Ups:
    • Re: Weird calculation
      • From: "Erik M. Buck" <email@hidden>
    • Re: Weird calculation
      • From: Donald Brown <email@hidden>
References: 
 >Re: Weird calculation (From: "Erik M. Buck" <email@hidden>)

  • Prev by Date: Window-closing sequence in a Document-based App
  • Next by Date: Re: Cocoa DropShell Wanted (Was Re: Implementing Open Document AE in Cocoa)
  • Previous by thread: Re: Weird calculation
  • Next by thread: Re: Weird calculation
  • Index(es):
    • Date
    • Thread