Skip to content

Instantly share code, notes, and snippets.

@tsbob
Created July 26, 2012 07:03
Show Gist options
  • Save tsbob/3180668 to your computer and use it in GitHub Desktop.
Save tsbob/3180668 to your computer and use it in GitHub Desktop.
Singletons 实现
Here's what the GCD (and ARC) version looks like:
+ (id)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init]; // or some other init method
});
return _sharedObject;
}
//define at
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment