Last active
January 18, 2016 03:09
-
-
Save 0x5e/f015eb6cdb0cff49a674 to your computer and use it in GitHub Desktop.
非线程安全,避免生成多个实例
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
#define SINGLETON(__class) \ | |
+ (instancetype)sharedInstance; | |
#define DEF_SINGLETON(__class) \ | |
static __class * __singleton__ = nil; \ | |
+ (instancetype)sharedInstance { \ | |
static dispatch_once_t once; \ | |
dispatch_once(&once, ^{ \ | |
__singleton__ = [[super allocWithZone:NULL] init]; \ | |
}); \ | |
return __singleton__; \ | |
} \ | |
+ (id)allocWithZone:(struct _NSZone *)zone { \ | |
return [__class sharedInstance]; \ | |
} \ | |
- (id)copyWithZone:(struct _NSZone *)zone { \ | |
return [__class sharedInstance]; \ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment