Created
November 27, 2012 21:14
-
-
Save 0xc010d/4157064 to your computer and use it in GitHub Desktop.
IB compliant Singleton in ARC
This file contains 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 : NSObject | |
+ (instancetype)sharedInstance; | |
@end |
This file contains 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 | |
+ (instancetype)sharedInstance { | |
__strong static id _sharedInstance = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_sharedInstance = [[self _alloc] _init]; | |
}); | |
return _sharedInstance; | |
} | |
+ (instancetype)allocWithZone:(NSZone *)zone { | |
return [self sharedInstance]; | |
} | |
+ (instancetype)alloc { | |
return [self sharedInstance]; | |
} | |
- (instancetype)init { | |
return self; | |
} | |
+ (instancetype)_alloc { | |
return [super allocWithZone:NULL]; | |
} | |
- (instancetype)_init { | |
return [super init]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment