Skip to content

Instantly share code, notes, and snippets.

@0x5e
Last active January 18, 2016 03:09
Show Gist options
  • Save 0x5e/f015eb6cdb0cff49a674 to your computer and use it in GitHub Desktop.
Save 0x5e/f015eb6cdb0cff49a674 to your computer and use it in GitHub Desktop.
非线程安全,避免生成多个实例
#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