OC语言day08-22宏定义抽取单例

pragma mark 宏定义抽取单例

pragma mark 概念

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];    // [ [alloc]  init]  // allocWithZone
    Tools *t3 = [Tools shareTools];
    NSLog(@" %p %p %p",t1,t2,t3);
    
    Tools *t4 = [t3 copy];
    Tools *t5 = [t3 mutableCopy];;
    NSLog(@"%p %p %p %p %p",t1,t2,t3,t4,t5);
    NSLog(@"------1.end -------");
    
    Person *p1 = [Person sharePerson];
    Person *p2 = [Person sharePerson];
    Person *p3 = [Person sharePerson];
    
    NSLog(@"%p ,%p, %p",p1,p2,p3);
    
    // 如何判断 当前是ARC 还是 MRC
    // 可以在编译的时候, 判断当前 是不是 ARC
     
#pragma mark - 在编译的时候 判断当前 是不是 ARC
#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>

// share + 当前的类名
//+ (instancetype)shareTools;

interSingleton(Tools);


@end
Tools.m
#import "Tools.h"

@implementation 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]init];
    });
    return _instance;

}

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

    return _instance;

}

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

#pragma mark MRC (才需要下面几行代码) //需要编译的时候 动态的 判断 当前是ARC 还是 MRC
- (oneway void)release
{
}

- (instancetype)retain
{
    return _instance;
}

- (NSUInteger)retainCount
{

    return MAXFLOAT;
}
*/

implementationSingleton(Tools)

@end

Person.h //人类
#import <Foundation/Foundation.h>
#import "Singleton.h"

@interface Person : NSObject

interSingleton(Person);

@end
Person.m
#import "Person.h"

@implementation Person

implementationSingleton(Person)

@end

Singleton.h //单例宏的文件.h

// 用 A 替换 B
//#define A B
// 以后 就可以使用 interSingleton 来代替后面的方法声明
//#define interSingleton + (instancetype)shareTools;

#pragma 1. - 定义 单例 宏的声明
// 传递一个参数的 宏定义 [宏的参数 用(xxx)  替代的参数用## xxx ]
#define interSingleton(name) + (instancetype)share##name

#pragma 2. - 定义 单例 宏的视线
// 使用 \ 反斜杠 就是换行 说明 下一行 也是注释
#if __has_feature(objc_arc)
// 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]init];   \
}); \
return _instance;\
}   \
- (id)copyWithZone:(NSZone *)zone   \
{   \
return _instance;   \
}\
- (id)mutableCopyWithZone:(NSZone *)zone    \
{   \
return _instance;   \
}   \


#else
// MRC

#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]init];   \
}); \
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



推荐阅读更多精彩内容