• 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: Singletons?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Singletons?


  • Subject: Re: Singletons?
  • From: Gregory Weston <email@hidden>
  • Date: Fri, 21 Jun 2002 17:23:43 -0400

On 6/21/02 at 8:05 AM, Christian Brunschen <email@hidden> wrote:

> A static variable is only visible in file scope - i.e., only in the file
> where it is defined. So a static variable declared & defined in the class
> implementation is not visible to anything outside that class. Thus, there
> is no pollution of global namespace, nor can anyone else refer to the
> variable and change it from underneath you.
>
> @implementation Foo
>
> static Foo *sharedInstance = nil;
>
> + sharedInstance {
> if (sharedInstance == nil) {
> sharedInstance = [[self alloc] init];
> }
>
> return sharedInstance;
> }
>
> // ... rest of class ...
>
> @end
>
> .... will work fine.

Also note that the variable can be moved inside the sharedInstance method:

+ (id)sharedInstance
{
static Foo* sSharedInstance = nil;
if (sharedInstance == nil)
{
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
_______________________________________________
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: Singletons?
      • From: Michael Gersten <email@hidden>
  • Prev by Date: Re: Panels shared by documents
  • Next by Date: Re: Panels shared by documents
  • Previous by thread: Re: Singletons?
  • Next by thread: Re: Singletons?
  • Index(es):
    • Date
    • Thread