-
-
Save intari/e62ccd4c60eb36eabc82 to your computer and use it in GitHub Desktop.
Singleton (Objective-C with ARC)
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 MySingleton : NSObject | |
// ... | |
+ (instancetype) sharedInstance; | |
+ (instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead"))); | |
- (instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead"))); | |
+ (instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead"))); | |
// ... | |
@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
@implimentation MySingleton | |
// ... | |
+ (instancetype) sharedInstance { | |
static id sharedInstance = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedInstance = [[super alloc] initInstance]; | |
}); | |
return sharedInstance; | |
} | |
- (instancetype) initInstance { | |
self = [super init]; | |
// Do any other initialisation stuff here | |
// ... | |
return self; | |
} | |
// ... | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment