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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ARC和MRC兼容的單例模式

2019-11-14 19:23:16
字體:
供稿:網(wǎng)友
一、ARC下的單例實現(xiàn)

  說明:在用戶實例化的方法控制單次執(zhí)行,同時開放單例的初始化方法。

-(instancetype)init{    self=[super init];    if(self){        static dispatch_once_t onceToken;        dispatch_once(&onceToken, ^{        });    }    return self;    }static id instance; +(instancetype)allocWithZone:(struct _NSZone *)zone{     static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{     instance=[super allocWithZone:zone];     }); return instance;}   + (instancetype) shareAudio{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{     instance=[[self alloc]init];     });     return instance; }  +(id)copyWithZone:(struct _NSZone *)zone{     return instance;}
二、MRC下的單例實現(xiàn)

   說明:在用戶實例化的方法控制單次執(zhí)行,同時開放單例的初始化方法,由于當前為MRC所以需要控制參內(nèi)存管理的方法單次執(zhí)行,因此相比ARC需要增加:

static id instance; +(instancetype)allocWithZone:(struct _NSZone *)zone{     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         instance=[super allocWithZone:zone];     });     return instance; }   + (instancetype) shareAudio{    static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         instance=[[self alloc]init];     });     return instance; } -(oneway void)release{ }  -(instancetype)autorelease{     return instance; }  -(instancetype)retain{     return instance; }  -(NSUInteger)retainCount{     return 1; }
三、兼容MRC 和ARC的宏定義

   說明:為了方便后期的引用,可以將單例抽取為宏定義,鑒于抽取的時候考慮到當前的手動計數(shù)和自動計數(shù)因此引入條件編譯:

#if !__has_feature(objc_arc)======當前是ARC#else======當前是MRC#endif代碼:#define singleton_h(name)  + (instancetype) share##name;#if !__has_feature(objc_arc)#define singleton_m(name)/ static id instance;/+(instancetype)allocWithZone:(struct _NSZone *)zone{/    static dispatch_once_t onceToken;/    dispatch_once(&onceToken, ^{/        instance=[super allocWithZone:zone];/    });/    return instance;/}///+ (instancetype) share##name{/    static dispatch_once_t onceToken;/    dispatch_once(&onceToken, ^{/        instance=[[self alloc]init];/    });/    return instance;/}/-(oneway void)release{/}//-(instancetype)autorelease{/    return instance;/}//-(instancetype)retain{/    return instance;/}//+(id)copyWithZone:(struct _NSZone *)zone{/    return instance;/}/-(NSUInteger)retainCount{/    return 1;/}#else#define singleton_m(name)/static id instance;/+(instancetype)allocWithZone:(struct _NSZone *)zone{/  static dispatch_once_t onceToken;/  dispatch_once(&onceToken, ^{/  instance=[super allocWithZone:zone];/});/  return instance;/}///+ (instancetype) share##name{/  static dispatch_once_t onceToken;/  dispatch_once(&onceToken, ^{/   instance=[[self alloc]init];/  });/  return instance;/}//+(id)copyWithZone:(struct _NSZone *)zone{/   return instance;/}#endif
四、文件引用

1 在.h文件引用singleton_h(audio);

2 在.m文件引用singleton_m(audio);

 

作者:杰瑞教育
出處:http://m.survivalescaperooms.com/jerehedu/ 
本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嵊州市| 攀枝花市| 高碑店市| 通辽市| 称多县| 醴陵市| 汤原县| 原平市| 涟源市| 确山县| 临沧市| 交城县| 蚌埠市| 宜章县| 磴口县| 福海县| 淳化县| 高台县| 海南省| 井冈山市| 锦州市| 江油市| 永济市| 达尔| 铜陵市| 宁化县| 鄯善县| 筠连县| 囊谦县| 江永县| 林州市| 宕昌县| 荥经县| 绥阳县| 金堂县| 韩城市| 桃园县| 黎川县| 英德市| 托里县| 开江县|