第08天OC语言(22):单例宏抽取

• 不要等到明天，明天太遥远，今天就行动。

一、本章笔记
`````` #define 左边 替换 右边的
传递参数 可以用(xx) 右边用##接收
>>> .m的抽取
分为ARC 和 MRC处理
动态判断MRC
``````
二、code
main.m
``````#pragma mark 22-单例宏抽取
#pragma mark 概念
/*

#define 左边 替换 右边的
传递参数 可以用(xx) 右边用##接收
>>> .m的抽取
分为ARC 和 MRC处理
动态判断MRC
*/
#pragma mark - 代码
#import <Foundation/Foundation.h>
#pragma mark 类
#import "Tools.h"
#import "Person.h"
#pragma mark - main函数
int main(int argc, const char * argv[])
{
Tools *t1 = [[Tools alloc]init]; // 内部会调用allocWithZone
Tools *t2 = [Tools new];
Tools *t3 = [Tools shareTools];
Tools *t4 = [Tools copy];
Tools *t5 = [Tools mutableCopy];
NSLog(@"%p,%p,%p,%p,%p",t1,t2,t3,t4,t5);
Person *p = [Person sharePerson];
Person *p1 = [Person sharePerson];
Person *p2 = [Person sharePerson];
NSLog(@"%p,%p,%p",p,p1,p2);

// 如何判断是ARC 还是 MRC呢?
#if __has_feature(objc_arc)
NSLog(@"ARC");
#else
NSLog(@"MRC");
#endif
return 0;
}

``````
Tools
``````>>>.h
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface Tools : NSObject<NSCopying,NSMutableCopying>
//+ (instancetype)shareToos;
interfaceSingleton(Tools);

@end

>>>.m
#import "Tools.h"

@implementation Tools
implementationSingleton(Tools)
/*
+ (instancetype)shareTools
{
Tools *instance = [[self alloc]init];
return instance;
}

static Tools *_instance = nil;
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
return _instance;

}

- (id)copyWithZone:(NSZone *)zone
{
return _instance;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
return _instance;
}

#pragma mark MRC需要多处理
- (oneway void)release
{

}
- (instancetype)retain
{
return _instance;
}
- (NSUInteger)retainCount
{
return MAXFLOAT;
}
*/
@end

``````
Person
``````>>>.h
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface Person : NSObject
interfaceSingleton(Person);
@end

>>>.m
#import "Person.h"

@implementation Person
implementationSingleton(Person)

@end

``````
Singleton
``````>>>.h
// >>> .h的抽取
//#define 左边 替换 右边的
// 传递参数 可以用(xx) 右边用##接收

// >>> .m的抽取
// 分为ARC 和 MRC处理
// 动态判断MRC
/*
#if __has_feature(objc_arc)
NSLog(@"ARC");
#else
NSLog(@"MRC");
#endif
*/

/*
#define implementationSingleton(name) \
+ (instancetype)share##name\
{\
name *instance = [[self alloc]init];\
return instance;\
}\
static name *_instance = nil;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (oneway void)release\
{\
\
}\
- (instancetype)retain\
{\
return _instance;\
}\
- (NSUInteger)retainCount\
{\
return MAXFLOAT;\
}\
*/

#define interfaceSingleton(name) + (instancetype)share##name

#if __has_feature(objc_arc)
#define implementationSingleton(name) \
+ (instancetype)share##name\
{\
name *instance = [[self alloc]init];\
return instance;\
}\
static name *_instance = nil;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
#else
#define implementationSingleton(name) \
+ (instancetype)share##name\
{\
name *instance = [[self alloc]init];\
return instance;\
}\
static name *_instance = nil;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (oneway void)release\
{\
\
}\
- (instancetype)retain\
{\
return _instance;\
}\
- (NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
#endif

``````