标签:
ARC&MRC都可使用的单例宏
//单例宏(懒汉式)的抽取,##可以拼接单个字符串,/可以拼接多个字符串
//.h文件的抽取
#define SHARED_INTERFACE(className) +(instancetype)shared##className;
//.m文件的抽取
#if !__has_feature(objc_arc)
#define SHARED_IMPLEMENTATION(className)\
static id instace;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instace=[super allocWithZone:zone];\
});\
return instace;\
}\
+(instancetype)shared##className{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instace=[[self alloc]init];\
});\
return instace;\
}\
-(id)copyWithZone:(NSZone *)zone{\
return instace;\
}\
-(NSUInteger)retainCount{\
return ULONG_MAX;\
}\
-(instancetype)retain{\
return instace;\
}\
-(oneway void)release{\
}\
-(instancetype)autorelease{\
return instace;\
}
//ARC
#else
#define SHARED_IMPLEMENTATION(className)\
static id instace;\
+(instancetype)allocWithZone:(struct _NSZone *)zone{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instace=[super allocWithZone:zone];\
});\
return instace;\
}\
+(instancetype)shared##className{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
instace=[[self alloc]init];\
});\
return instace;\
}\
-(id)copyWithZone:(NSZone *)zone{\
return instace;\
}
#endif
标签:
原文地址:http://www.cnblogs.com/lijianyi/p/4278441.html