书写一个完整的单例模式类,并封装为宏
来源:互联网 发布:sem和seo 编辑:程序博客网 时间:2024/05/17 00:56
直接在.h文件中书写下列宏定义,然后在需要书写单例模式的.m文件下,导入Singleton.h文件,然后直接调用Singleton_h(xxx)即可
xxx写自己起的名字
#define Singleton_h(name) +(instancetype)shared##name;
# if __has_feature(objc_arc)
#define Singleton_m(name) static id _instance;\
\
+(instancetype)shared##name{\
\
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)copyWithZone:(NSZone *)zone{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}
# else
#define Singleton_m(name) static id _instance;\
\
+(instancetype)shared##name{\
\
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)copyWithZone:(NSZone *)zone{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone{\
return _instance;\
}\
\
-(oneway void)release{\
\
}\
\
-(instancetype)retain{\
return _instance;\
}\
\
-(instancetype)autorelease{\
return _instance;\
}\
\
-(NSUInteger)retainCount{\
return 1;\
}
#endif
0 0
- 书写一个完整的单例模式类,并封装为宏
- 一个完整的单例模式代码
- php基于单例模式封装mysql类完整实例
- php基于单例模式封装mysql类完整实例
- PHP封装的一个单例模式Mysql操作类
- 单例的书写
- 封装的一个完整的分页类
- 引用 一个完整的JDBC封装类
- 完整单例模式
- 完整单例模式的写法
- 单例模式最完整的讲解
- 单例模式完整的实现
- Android 单例设计模式书写
- Okhttp的单例模式封装
- 封装好的单例模式
- 将MFC 中的类封装为一个Dll并导出
- 一个封装比较完整的FTP类——clsFTP
- 一个封装比较完整的FTP类——clsFTP
- spring文件报错,报错内容 - cvc-complex-type.2.4.c
- 数组
- servlet底层实现
- 一个多线程Ping 类
- 深入理解Lua的闭包一:概念和应用
- 书写一个完整的单例模式类,并封装为宏
- 接口的设计应该从小到大,还是从大到小?
- C和指针读书笔记(第十一章)
- C Primer Plus学习 二十七 string.h strcat()函数
- Intel Code Challenge Final Round C. Ray Tracing 拓展欧几里得或模拟 (好题)
- 【数据结构】算法9.13-9.14 B树-插入&查找
- 集合的二进制表示
- java 字符串转成 json 数组并且遍历
- hdu1151air raid