Created
June 8, 2012 21:05
-
-
Save flipjorge/2898116 to your computer and use it in GitHub Desktop.
iOS Singleton snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Singleton | |
+(Singleton*)sharedSingleton; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation Singleton | |
static Singleton *_sharedSingleton; | |
+(Singleton*)sharedSingleton{ | |
@synchronized([Singleton class]) | |
{ | |
if(!_sharedSingleton) | |
_sharedSingleton = [[self alloc] init]; | |
return _sharedSingleton; | |
} | |
return nil; | |
} | |
+(id)alloc | |
{ | |
@synchronized([Singleton class]) | |
{ | |
NSAssert(_sharedSingleton == nil, @"Attempted to allocate a second instance of the Singleton singleton"); | |
_sharedSingleton = [super alloc]; | |
return _sharedSingleton; | |
} | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment