Last active
September 20, 2015 12:32
-
-
Save alexlee002/f73555aa57fe20642a0c to your computer and use it in GitHub Desktop.
A safe Objective-C singleton implementation. All instance method such as [[Singleton alloc] init], [[Singletion allocWithZone:zone] init]; [Singleton sharedInstance] are return the same instance and you can call them multi-times.
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
// | |
// Singleton_Template.h | |
// | |
// Created by Alex Lee on 3/11/15. | |
// | |
#undef AS_SINGLETON | |
#define AS_SINGLETON \ | |
+ (instancetype)sharedInstance; \ | |
+ (void)destroy; | |
#if __has_feature(objc_arc) | |
# undef SYNTHESIZE_SINGLETON | |
# define SYNTHESIZE_SINGLETON \ | |
static id __singleton_instance__ = nil; \ | |
+ (instancetype)sharedInstance { \ | |
@synchronized(self) { \ | |
if (__singleton_instance__) { \ | |
return __singleton_instance__; \ | |
} \ | |
} \ | |
return [[self alloc] init]; \ | |
} \ | |
\ | |
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \ | |
static dispatch_once_t onceToken; \ | |
dispatch_once( &onceToken, ^{ __singleton_instance__ = [super allocWithZone:zone]; } ); \ | |
return __singleton_instance__; \ | |
} \ | |
\ | |
+ (instancetype)alloc { \ | |
return [self allocWithZone:NULL]; \ | |
} \ | |
\ | |
+ (instancetype)new { \ | |
return [self allocWithZone:NULL]; \ | |
} \ | |
\ | |
- (id)copy { return self; } \ | |
- (id)mutableCopy { return self; } \ | |
\ | |
+ (void)destroy { \ | |
__singleton_instance__ = nil; \ | |
} | |
#else | |
# undef SYNTHESIZE_SINGLETON | |
# define SYNTHESIZE_SINGLETON \ | |
static id __singleton_instance__ = nil; \ | |
+ (instancetype)sharedInstance { \ | |
return [[self alloc] init]; \ | |
} \ | |
\ | |
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \ | |
static dispatch_once_t onceToken; \ | |
dispatch_once( &onceToken, ^{ __singleton_instance__ = [super allocWithZone:zone]; } ); \ | |
return __singleton_instance__; \ | |
} \ | |
\ | |
+ (instancetype)alloc { \ | |
return [self allocWithZone:NULL]; \ | |
} \ | |
\ | |
+ (instancetype)new { \ | |
return [self allocWithZone:NULL]; \ | |
} \ | |
\ | |
- (id)copy { return self; } \ | |
- (id)mutableCopy { return self; } \ | |
\ | |
+ (id)copyWithZone:(struct _NSZone *) { return self; } \ | |
+ (id)mutableCopyWithZone:(struct _NSZone *) { return self; } \ | |
\ | |
- (instancetype)retain { return self; } \ | |
- (oneway void)release {} \ | |
- (instancetype)autorelease { return self; }\ | |
- (NSUInteger)retainCount { return NSUIntegerMax; } \ | |
\ | |
+ (void)destroy { \ | |
[__singleton_instance__ dealloc]; \ | |
__singleton_instance__ = nil; \ | |
} \ | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment