• 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: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: Hamish Allan <email@hidden>
  • Date: Sat, 26 Nov 2005 19:21:25 +0000


Have I missed some important detail in this debate? It is starting to feel like I must have! Or is everyone missing the wood for the trees?


The [[NSFoo] sharedFoo] technique works for most situations that require the singleton pattern, and is very simple to implement. It does not guarantee that the shared instance will never be released, but then again if you don't follow the rules about alloc/copy/retain/ release you're on your own anyway. If you need to use your singleton as a key in a dictionary or something which would copy it, just do what you would do if you were writing an immutable class and retain it instead. If you're concerned that people using your class will do things they shouldn't, just write helpful "warn if instance already allocated" wrappers around e.g., allocWithZone:.

Best wishes,
Hamish

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: CVS: Only the linking fails, but how do we overcome this?
  • Next by Date: Re: Is Apple's singleton sample code correct?
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread