As shown below:
//This section below is used directly in the macro test#define SYNTHESIZE_SINGLETON_FOR_HEADER(className) \ \ + (className *)sharedInstance;\ + (void)destroyInstance; //OnceToken = 0 before singleton generation, onceToken = -1 after singleton generation, and then keep the value of -1. After knowing this, I think you should have a idea.#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;\ }\
//Usage, be careful to follow the NSCoping and NSMutableCopying protocolsimport <Foundation/> @interface YNHTUserModel : NSObject<NSCopying,NSMutableCopying> SYNTHESIZE_SINGLETON_FOR_HEADER(YNHTUserModel); @property (nonatomic,copy) NSString* inviter_id;//Inviter ID@property (nonatomic,copy) NSString* token; @property (nonatomic,copy) NSString* nick_name; @end
#import "" @implementation YNHTUserModel SYNTHESIZE_SINGLETON_FOR_CLASS(YNHTUserModel); @end
The above complete examples of using ios single interest and destruction macro definition is all the content I share with you. I hope you can give you a reference and I hope you can support me more.