您的位置:首页 > 移动开发 > IOS开发

IOS开发之_快速单例

2016-01-14 00:00 417 查看
/** .h */
#define singleton_interface(class) + (instancetype) shared##class;

/** .m */
#define singleton_implementation(class) \
static class *_instance; \
\
+ (id) allocWithZone: (struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once( &onceToken, ^{ \
_instance = [super allocWithZone: zone]; \
}); \
return _instance; \
} \
\
+ (instancetype) shared##class \
{ \
if ( _instance == nil) { \
_instance = [[class alloc] init]; \
} \
return _instance; \
} \
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: