您好,登錄后才能下訂單哦!
如下所示:
//下面這段宏考過去直接用 #define SYNTHESIZE_SINGLETON_FOR_HEADER(className) \ \ + (className *)sharedInstance;\ + (void)destroyInstance; //在單例生成之前onceToken = 0,在單例生成之后onceToken = -1了,之后一直保持-1這個值,知道這個之后我想你應該有思路了 #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 協議 import <Foundation/Foundation.h> @interface YNHTUserModel : NSObject<NSCopying,NSMutableCopying> 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" @implementation YNHTUserModel SYNTHESIZE_SINGLETON_FOR_CLASS(YNHTUserModel); @end
以上這篇ios 單利的完整使用實例 及銷毀 宏定義就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。