iOS开发之单例使用(宏)
2016-06-01 17:16
253 查看
.h文件中
#define SKSingletonH(__className)\
+ (__className *)shareInstance;\
.m文件中
#define SKSingletonM(__className)\
\
static __className *_instance = nil;\
\
+ (__className *)shareInstance {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
- (id)copy {\
return _instance;\
}\
\
- (id)mutableCopy {\
return _instance;\
}\
#define SKSingletonH(__className)\
+ (__className *)shareInstance;\
.m文件中
#define SKSingletonM(__className)\
\
static __className *_instance = nil;\
\
+ (__className *)shareInstance {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
- (id)copy {\
return _instance;\
}\
\
- (id)mutableCopy {\
return _instance;\
}\
相关文章推荐
- 设计模式之创建型模式 - 特别的变量问题
- C#单例模式(Singleton Pattern)实例教程
- php设计模式之单例、多例设计模式的应用分析
- javascript 单例/单体模式(Singleton)
- C++单例模式应用实例
- 浅谈Java编程中的单例设计模式
- Python单例模式实例分析
- Java单例模式实例简述
- 新手谈设计模式 - 单例模式
- JAVA语言泛型编程实现单例模式
- Swift之单例模式
- iOS设计模式之单例模式
- python logging 单例模式范例
- 单例模式
- 单例模式扩展
- 设计模式1-单例设计模式
- iOS 单例模式全面解析
- C#窗口实现单例模式
- 单例的宏模板
- 单例-------OC中单例 / Swift中单例