国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一句話實現OC單例模式

2019-11-14 18:43:21
字體:
來源:轉載
供稿:網友

首先新建一個頭文件,定義如下宏:

// .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

然后在你定義單例類的

.h 文件 寫 SingletonH(MyMethodName) 

.m 文件寫 SingletonM(MyMethodName)

搞定!

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安仁县| 老河口市| 大余县| 弥渡县| 安福县| 德阳市| 赤水市| 徐水县| 台北县| 延津县| 隆德县| 水城县| 衡水市| 菏泽市| 洛隆县| 凤山县| 思茅市| 丹寨县| 资中县| 巴马| 吐鲁番市| 彝良县| 株洲县| 洞头县| 山丹县| 漳平市| 孙吴县| 朔州市| 通城县| 临夏县| 区。| 贡嘎县| 镇巴县| 瑞金市| 武定县| 确山县| 金坛市| 吐鲁番市| 淮南市| 甘谷县| 治多县|