• 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: Writing a method like "sharedApplication"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing a method like "sharedApplication"


  • Subject: Re: Writing a method like "sharedApplication"
  • From: "John C. Warner" <email@hidden>
  • Date: Wed, 19 Jan 2005 22:32:20 -0500


On 19 Jan, 2005, at 10:15 PM, Todd Yandell wrote:

Hi,

I have a simple question that haven't been able to find a solution for. What I would like to do is create a method that works like NSApplication's "sharedApplication" message, or NSUserDefault's "sharedUserDefaults" message. The first time you call it it should create an instance of itself and keep a reference to it somehow. After that, anytime the method is called it should simply return the the instance it created earlier. This part is easy, I can just use a static variable. The part I'm not sure on however, is how to release it when the app exits. I've considered C's "atexit" function, but I thought there may be an all-Cocoa way of doing this. Any ideas?

You want a singleton. See <http://www.cocoadev.com/index.pl?SingletonDesignPattern>


As far as releasing it, it'll be released anyway when the app exits, so there's no real need to unless you need to do some other cleanup, in which case you can register for NSApplication's applicationWillTerminate: notification.

-John

_______________________________________________
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


  • Follow-Ups:
    • Re: Writing a method like "sharedApplication"
      • From: Todd Yandell <email@hidden>
References: 
 >Writing a method like "sharedApplication" (From: Todd Yandell <email@hidden>)

  • Prev by Date: Re: Why do "loose" nibs take precedence over nibs in .lproj?
  • Next by Date: Re: Writing a method like "sharedApplication"
  • Previous by thread: Writing a method like "sharedApplication"
  • Next by thread: Re: Writing a method like "sharedApplication"
  • Index(es):
    • Date
    • Thread