• 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
Calling the same function in multiple classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calling the same function in multiple classes


  • Subject: Calling the same function in multiple classes
  • From: Andrew Merenbach <email@hidden>
  • Date: Mon, 4 Nov 2002 21:28:52 -0800

In a program I'm making, a matrix of buttons in a window (each with a different tag) causes the contents of the window to change (to a new "feature"), and when the user returns to the main screen the content view is changed back. I currently have code that looks like this:

- (IBAction)changeMainView:(id)sender
{
switch([[sender selectedCell] tag]) {
case 0: [[CBMolarityController sharedInstance] showView]; break;
case 1: [[CBDilutionController sharedInstance] showView]; break;
case 2: [[CBSolubilityController sharedInstance] showView]; break;
case 3: [[CBConcentrationController sharedInstance] showView]; break;
case 4: [[CBNormalityController sharedInstance] showView]; break;
case 5: [[CBTitrationController sharedInstance] showView]; break;
case 6: [[CBMolalityController sharedInstance] showView]; break;
}
}

I had the bright idea that it might run more quickly if I were to do the following:

- (IBAction)changeMainView:(id)sender
{
static NSArray *solutionsArray;

if (!solutionsArray) solutionsArray = [[NSArray alloc] initWithObjects:[CBMolarityController sharedInstance],[CBDilutionController sharedInstance],[CBSolubilityController sharedInstance],[CBConcentrationController sharedInstance],[CBNormalityController sharedInstance],[CBTitrationController sharedInstance],[CBMolalityController sharedInstance]];

[[solutionsArray objectAtIndex:[[sender selectedCell] tag]] showView];
}

It looks convoluted, but runs well. Is there any particular reason--other than the obvious "it's convoluted!"--not to use it? Most notably, will I most definitely *not* have a speed improvement with the second method? Or is it even poor coding?

Take care,
Andrew Merenbach
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Calling the same function in multiple classes
      • From: Bill Cheeseman <email@hidden>
  • Prev by Date: Re: Cocoa really needs to be thread-safe
  • Next by Date: Re: Terminating an Application ?
  • Previous by thread: Re: NSDocuments without window associations
  • Next by thread: Re: Calling the same function in multiple classes
  • Index(es):
    • Date
    • Thread