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

【iOS开发系列】单例模式宏(兼容ARC和非ARC)

2015-06-24 19:28 591 查看
实现单例模式。这种代码完全不需要自己写的,只要拷贝就好了。在这里分享一下。
/**
*  ##:连接字符串和参数
*/
#define singleton_h(name) + (instancetype)shared##name;

/**
*  【 ARC】环境
*/
#if __has_feature(objc_arc)

#define singleton_m(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:(struct _NSZone *)zone                   \
{                                                           \
return _instance;                                       \
}

/**
*  非【 ARC】环境
*/
#else
#define singleton_m(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;                                       \
}                                                           \
\
- (oneway void)release                                      \
{                                                           \
\
}                                                           \
\
- (id)autorelease                                           \
{                                                           \
return _instance;                                       \
}                                                           \
\
- (id)retain                                                \
{                                                           \
return _instance;                                       \
}                                                           \
\
- (id)retainCount                                           \
{                                                           \
return 1;                                               \
}                                                           \
\
+ (id)copyWithZone:(struct _NSZone *)zone                   \
{                                                           \
return _instance;                                       \
}

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