博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oc85--利用宏定义简化单例
阅读量:6396 次
发布时间:2019-06-23

本文共 3294 字,大约阅读时间需要 10 分钟。

//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 
#import "Singleton.h"@interface Tools : NSObject
// 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 
#import "Singleton.h"@interface Person : NSObjectinterfaceSingleton(Person); ////用Singleton.h里面的内容替换。@end
////  Person.m#import "Person.h"@implementation PersonimplementationSingleton(Person)    //用Singleton.h里面的内容替换。@end
////  main.m//  宏定义抽取单例,宏定义本质是替换,简化代码,用公用的代码进行替换。//#import 
#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;}

 

转载地址:http://xcrha.baihongyu.com/

你可能感兴趣的文章
IPSec逻辑体系架构
查看>>
Exchange 2013部署系列之(六)配置邮件流和客户端访问
查看>>
List of Free Programming books
查看>>
思考Android架构(二):像Android框架,如何(How-to)吸引开发者来使用它呢?
查看>>
windows 8 应用小技巧(36-40)
查看>>
8. package 和 import
查看>>
在html中,怎么获取当前页面body的高度,body是没有设置高度的,但是里面有内容...
查看>>
IDC云时代神兵利器-还在等什么!是IDC就可以云主机
查看>>
把 Array 转换成 Map
查看>>
MyBatis入门学习
查看>>
ASA防火墙IPSEC
查看>>
djangostart01
查看>>
NoSql之深入浅出redis
查看>>
Ubuntu 12.04无法关机、重启解决办法
查看>>
[算法]C语言实现 骑士旅游(递归)
查看>>
mysql主从复制要点02
查看>>
HTML5 LocalStorage 本地存储
查看>>
froont在线响应式布局工具
查看>>
深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
查看>>
superword-mode一览
查看>>