單例抽取成宏

代碼能夠下載,連接在最下面,也能夠直接複製下面的代碼,本身新建一個類。spa

用法及其簡單。例如it

.h文件io

#import <Foundation/Foundation.h>import

//引入頭文件下載

#import "singleton.h"im

@interface DBTools : NSObjectstatic

//.h的宏di

singleton_h(DBTools)文件

@endcopy

.m文件

#import "DBTools.h"

 

@implementation DBTools

//.m的宏

singleton_m(DBTools)

@end

 

 

//============這是抽取的內容=================

 

#if __has_feature(objc_arc) //ARC

//.h 拼接參數使用##

#define singleton_h(name) +(instancetype)sharad##name;

 

//.m 宏裏面換行

#define singleton_m(name) static id _instanceType;\

+(instancetype)sharad##name\

{\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

_instanceType = [[self alloc]init];\

});\

return _instanceType;\

}\

+ (instancetype)allocWithZone:(struct _NSZone *)zone\

{\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

_instanceType = [super allocWithZone:zone];\

});\

return _instanceType;\

}\

-(id)copyWithZone:(NSZone *)zone\

{\

return _instanceType;\

}

 

 

#else //MRC

//.h 拼接參數使用##

#define singleton_h(name) +(instancetype)sharad##name;

 

//.m 宏裏面換行

#define singleton_m(name) static id _instanceType;\

+(instancetype)sharad##name\

{\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

_instanceType = [[self alloc]init];\

});\

return _instanceType;\

}\

+ (instancetype)allocWithZone:(struct _NSZone *)zone\

{\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

_instanceType = [super allocWithZone:zone];\

});\

return _instanceType;\

}\

-(id)copyWithZone:(NSZone *)zone\

{\

return _instanceType;\

}\

-(oneway void)release\

{\

    \

}\

-(instancetype)retain\

{\

    return _instanceType;\

}\

-(instancetype)autorelease\

{\

    return _instanceType;\

}\

- (NSUInteger)retainCount\

{\

    return 1;\

}

 

 

 

#endif

 

 

 

下載連接地址⬇️

http://pan.baidu.com/s/1mhKHg7Y

相關文章
相關標籤/搜索