欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 单利的完整使用实例 及销毁 宏定义

程序员文章站 2023-12-19 11:54:28
如下所示: //下面这段宏考过去直接用 #define synthesize_singleton_for_header(classname) \ \ + (...

如下所示:

//下面这段宏考过去直接用
#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 单利的完整使用实例 及销毁 宏定义就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: