ios 单利的完整使用实例 及销毁 宏定义
如下所示:
//下面这段宏考过去直接用
#defineSYNTHESIZE_SINGLETON_FOR_HEADER(className)\
\
+(className*)sharedInstance;\
+(void)destroyInstance;
//在单例生成之前onceToken=0,在单例生成之后onceToken=-1了,之后一直保持-1这个值,知道这个之后我想你应该有思路了
#defineSYNTHESIZE_SINGLETON_FOR_CLASS(className)\
\
staticclassName*shared##className=nil;\
staticdispatch_once_tonceToken;\
+(className*)sharedInstance\
{\
return[[selfalloc]init];\
}\
+(className*)allocWithZone:(struct_NSZone*)zone\
{\
dispatch_once(&onceToken,^{\
shared##className=[superallocWithZone:zone];\
});\
returnshared##className;\
}\
-(className*)copyWithZone:(NSZone*)zone\
{\
returnshared##className;\
}\
-(className*)mutableCopyWithZone:(NSZone*)zone\
{\
returnshared##className;\
}\
+(void)destroyInstance{\
shared##className=nil;\
onceToken=0;\
}\
//用法,注意要遵循NSCopying,NSMutableCopying协议 import@interfaceYNHTUserModel:NSObject SYNTHESIZE_SINGLETON_FOR_HEADER(YNHTUserModel); @property(nonatomic,copy)NSString*inviter_id;//邀请人ID @property(nonatomic,copy)NSString*token; @property(nonatomic,copy)NSString*nick_name; @end
#import"YNHTUserModel.h" @implementationYNHTUserModel SYNTHESIZE_SINGLETON_FOR_CLASS(YNHTUserModel); @end
以上这篇ios单利的完整使用实例及销毁宏定义就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短