• 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
static acting like __block in GCD singleton pattern
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

static acting like __block in GCD singleton pattern


  • Subject: static acting like __block in GCD singleton pattern
  • From: Matt Neuburg <email@hidden>
  • Date: Fri, 28 Jun 2013 17:17:48 -0700

In this sort of well-known code pattern for making a singleton with GCD:

+ (MyClass *) sharedInstance {
    static MyClass *sharedInstance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once (&onceToken, ^{
        sharedInstance = [MyClass new];
    });
    return sharedInstance;
}

Why is the block permitted to assign to the variable sharedInstance outside the block? Evidently it is because "static" has an effect like "__block", in that it makes the variable outside the block assignable. But how, exactly? Is it some kind of side effect of being a local static? Thx - m.

--
matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
A fool + a tool + an autorelease pool = cool!
Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do

_______________________________________________

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: static acting like __block in GCD singleton pattern
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: RE: Moving a textField from under keyboard
  • Next by Date: Re: static acting like __block in GCD singleton pattern
  • Previous by thread: Re: Moving a textField from under keyboard
  • Next by thread: Re: static acting like __block in GCD singleton pattern
  • Index(es):
    • Date
    • Thread