//Singleton.h
// 以后就可以使用interfaceSingleton来替代后面的方法声明。 \表示下一行也是上一行的内容。
#define interfaceSingleton(name) +(instancetype)share##name
#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
//
// Tools.h
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface Tools : NSObject<NSCopying, NSMutableCopying>
// share + 当前的类名
//+ (instancetype)shareTools;
interfaceSingleton(Tools); ////用Singleton.h里面的内容替换。
@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;
}
// MRC
- (oneway void)release
{
}
- (instancetype)retain
{
return _instance;
}
- (NSUInteger)retainCount
{
return MAXFLOAT;
}
*/
implementationSingleton(Tools) ////用Singleton.h里面的内容替换。
@end
//
// Person.h
//
#import <Foundation/Foundation.h>
#import "Singleton.h"
@interface Person : NSObject
interfaceSingleton(Person); ////用Singleton.h里面的内容替换。
@end
//
// Person.m
#import "Person.h"
@implementation Person
implementationSingleton(Person) //用Singleton.h里面的内容替换。
@end
//
// main.m
// 宏定义抽取单例,宏定义本质是替换,简化代码,用公用的代码进行替换。
//
#import <Foundation/Foundation.h>
#import "Tools.h"
#import "Person.h"
int main(int argc, const char * argv[]) {
Tools *t1 = [[Tools alloc] init]; //内部会调用allocWithZone
Tools *t2 = [Tools new];// [[alloc] init] allocWithZone
Tools *t3 = [Tools shareTools];
Tools *t4 = [t3 copy];
Tools *t5 = [t3 mutableCopy];
NSLog(@"%p, %p, %p, %p, %p", t1, t2, t3, t4, t5);//0x100300120, 0x100300120, 0x100300120, 0x100300120, 0x100300120
Person *p1 = [Person sharePerson];
Person *p2 = [Person sharePerson];
Person *p3 = [Person sharePerson];
NSLog(@"%p, %p, %p", p1 , p2, p3);//0x100202460, 0x100202460, 0x100202460
// 如何判断当前是ARC还是MRC?
// 可以在编译的时候判断当前是否是ARC
#if __has_feature(objc_arc)
NSLog(@"ARC");
#else
NSLog(@"MRC");
#endif
return 0;
}