• 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: Table View/Array Troubles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Table View/Array Troubles


  • Subject: Re: Table View/Array Troubles
  • From: Chris Hanson <email@hidden>
  • Date: Fri, 13 Jun 2008 02:03:13 -0700

On Jun 12, 2008, at 10:21 PM, Kyle Sluder wrote:

If you're implementing
the singleton pattern, for example, you would override -init
differently.

In most cases, I would strongly suggest *not* doing anything differently if you are making a singleton. If you want something to be a singleton, don't follow the "ensure there is only one instance of this class" example. Instead, just implement a +sharedWhatever method that returns a global, and does an +alloc/-init if necessary:


  @implementation Foo

  + (id)sharedFoo {
      static id sharedFoo = nil;

      if (sharedFoo == nil) {
          sharedFoo = [[self alloc] init];
      }

      return sharedFoo;
  }

  @end

No overrides of +allocWithZone:, -retain, -release, -autorelease or anything of the sort. (Obviously if you're writing code that will invoke +sharedFoo from multiple threads, you'll want to do appropriate synchronization as well.)

This will be much easier to write reasonable unit tests for, since you can still separately instantiate it (and therefore won't have to "reset" anything after each). You will also be prepared for cases where you need to promote something that starts as a singleton to a non-singleton.

  -- Chris

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Table View/Array Troubles (From: Gabriel Shahbazian <email@hidden>)
 >Re: Table View/Array Troubles (From: Bob Warwick <email@hidden>)
 >Re: Table View/Array Troubles (From: "Kyle Sluder" <email@hidden>)

  • Prev by Date: Re: Attack My Code! Part 1
  • Next by Date: Re: argument checks
  • Previous by thread: Re: Table View/Array Troubles
  • Next by thread: argument checks
  • Index(es):
    • Date
    • Thread