单例模式的宏
作者:
蒋昉霖 | 来源:发表于
2016-03-21 18:09 被阅读29次// .h文件
#define JXHSingletonH(name) + (instancetype)shared##name;
// .m文件
#define JXHSingletonM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
本文标题:单例模式的宏
本文链接:https://www.haomeiwen.com/subject/elpklttx.html
网友评论