• 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
Singletons on Leopard with GC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Singletons on Leopard with GC


  • Subject: Singletons on Leopard with GC
  • From: Karl Moskowski <email@hidden>
  • Date: Thu, 10 Apr 2008 17:11:20 -0400

I have a couple of classes for which I'd like to use the singleton pattern.

I read some of the old posts on this list and at cocoadev.com about approaches to take. They mention Apple's recommendations about overriding retain, release, etc. to essentially turn them into no-ops. However, they're from pre-Leopard days, and in a GC-required app, the compiler automatically no-ops those methods.

I'm currently using something like this code in my implementations:

static MyClass *sharedInstance;
+ (MyClass *)sharedInstance {
	if (!sharedInstance)
		sharedInstance = [[self alloc] init];
	return sharedInstance;
}

- (id)init {
	if (!sharedInstance)
		sharedInstance = [super init];
	return sharedInstance;
}

My goal is to ensure that [MyClass sharedInstance] and [[MyClass alloc] init] return the same singleton object. Furthermore, I'd like to ensure that an object instantiated in a NIB is the sharedInstance too. So far, it seems to work, but I'm afraid I've missed something.

Any suggestions? Thanks.

----
Karl Moskowski <email@hidden>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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

  • Follow-Ups:
    • Re: Singletons on Leopard with GC
      • From: Citizen <email@hidden>
  • Prev by Date: Re: System Preferences window is wider in Leopard? How do you make a Tiger compatible UI?
  • Next by Date: Re: Replacement for GetApplicationEventTarget?
  • Previous by thread: NSArrayController and managedObjectContext Problem
  • Next by thread: Re: Singletons on Leopard with GC
  • Index(es):
    • Date
    • Thread