- 不要等到明天,明天太遙遠,今天就行動。
須讀:看完該文章你能做什么?
用宏定義抽取單例
學習前:你必須會什么?(在這里我已經默認你具備C語言的基礎了)
什么是單例
一、本章筆記
#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