Created
December 16, 2013 12:39
-
-
Save EugeneRash/7986363 to your computer and use it in GitHub Desktop.
Modern Obj-C Singleton
(c)http://habrahabr.ru/users/code_monkey/
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 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 | |
@implementation MySingleton | |
+(instancetype) sharedInstance { | |
static dispatch_once_t pred; | |
static id shared = nil; | |
dispatch_once(&pred, ^{ | |
shared = [[super alloc] initUniqueInstance]; | |
}); | |
return shared; | |
} | |
-(instancetype) initUniqueInstance { | |
return [super init]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment