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

IOS第二天多线程-04简化单例模式

2015-09-09 16:27 330 查看
******HMSingleton-ARC.h

// .h文件
#define HMSingletonH(name) + (instancetype)shared##name;

// .m文件
#define HMSingletonM(name) \
static id _instance; \
\
+ (id)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; \
}


****.m的文件中

@implementation HMMovieTool
HMSingletonM(MovieTool)
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: