您好,登錄后才能下訂單哦!
如下所示:
//下面這段宏考過(guò)去直接用 #define SYNTHESIZE_SINGLETON_FOR_HEADER(className) \ \ + (className *)sharedInstance;\ + (void)destroyInstance; //在單例生成之前onceToken = 0,在單例生成之后onceToken = -1了,之后一直保持-1這個(gè)值,知道這個(gè)之后我想你應(yīng)該有思路了 #define SYNTHESIZE_SINGLETON_FOR_CLASS(className) \ \ static className *shared##className = nil; \ static dispatch_once_t onceToken;\ + (className *)sharedInstance\ {\ return [[self alloc] init];\ }\ + (className *)allocWithZone:(struct _NSZone *)zone\ {\ dispatch_once(&onceToken, ^{\ shared##className = [super allocWithZone:zone];\ });\ return shared##className;\ }\ - (className *)copyWithZone:(NSZone *)zone\ {\ return shared##className;\ }\ - (className *)mutableCopyWithZone:(NSZone *)zone\ {\ return shared##className;\ }\ + (void)destroyInstance {\ shared##className = nil;\ onceToken = 0;\ }\
//用法,注意要遵循NSCopying,NSMutableCopying 協(xié)議 import <Foundation/Foundation.h> @interface YNHTUserModel : NSObject<NSCopying,NSMutableCopying> SYNTHESIZE_SINGLETON_FOR_HEADER(YNHTUserModel); @property (nonatomic,copy) NSString* inviter_id;//邀請(qǐng)人ID @property (nonatomic,copy) NSString* token; @property (nonatomic,copy) NSString* nick_name; @end
#import "YNHTUserModel.h" @implementation YNHTUserModel SYNTHESIZE_SINGLETON_FOR_CLASS(YNHTUserModel); @end
以上這篇ios 單利的完整使用實(shí)例 及銷(xiāo)毀 宏定義就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。