单利宏的实现
来源:互联网 发布:最新台风下载软件 编辑:程序博客网 时间:2024/06/09 23:31
.h 文件 写 SingletonH(MyMethodName)
.m 文件写 SingletonM(MyMethodName)
// 帮助实现单例设计模式
// .h文件的实现
#define SingletonH(methodName) + (instancetype)shared##methodName;
// .m文件的实现
#if __has_feature(objc_arc) // 是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#else // 不是ARC
#define SingletonM(methodName) \
static id _instace = nil; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instace == nil) { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super allocWithZone:zone]; \
}); \
} \
return _instace; \
} \
\
- (id)init \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instace = [super init]; \
}); \
return _instace; \
} \
\
+ (instancetype)shared##methodName \
{ \
return [[self alloc] init]; \
} \
\
- (oneway void)release \
{ \
\
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return 1; \
} \
+ (id)copyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
} \
\
+ (id)mutableCopyWithZone:(struct _NSZone *)zone \
{ \
return _instace; \
}
#endif
- 单利宏的实现
- java实现线程安全的单利模式
- dispatch_once 实现单利
- 单利设计模式的几种实现方式
- java多线程环境下单利模式的实现
- java 单利模式的几种实现方法
- 线程安全的单利
- 单利的创建 iOS
- 单利模式的三要素
- iOS 单利的写法
- 单利模式的使用
- 单利
- 单利
- iOS 开发中“宏”的高级使用——单利
- 单利的三种办法
- iOS 单利的简单创建
- 单利的原理和使用方法
- iOS单利模式的创建
- win7和XP安装SQL Server 2008安装教程,及下载地址
- 自然博物馆记
- LeetCode Course Schedule
- 【第八章】 对ORM的支持 之 8.1 概述 ——跟我学spring3
- Java - Get object size
- 单利宏的实现
- Grails 3.0 报错CLI:BAD INIT
- TCP协议的三次握手和四次挥手
- 使用jquery插件layer代替window.confirm方法
- 基于开源框架Glide加载Gif资源图到Android ImageView中
- darknet网络中识别本地视频
- 【SQL 编程你也行】学习SQL Server的经历
- Android 在Fragment中使用TabLayout时标题不显示问题
- sql中替换函数(replace(),STUFF())的用法