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

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

//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;}

 

转载于:https://www.cnblogs.com/yaowen/p/7443802.html

你可能感兴趣的文章
选择器
查看>>
rownum 的使用
查看>>
Mysql与Oracle 的对比
查看>>
MVC系列博客之排球计分(三)模型类的实现
查看>>
npm安装
查看>>
阅读笔记02
查看>>
2019年春季学期第二周作业
查看>>
2014北邮计算机考研复试上机题解(上午+下午)
查看>>
mySQL 教程 第7章 存储过程和函数
查看>>
OGG同步Oracle到Kafka(Kafka Connect Handler)
查看>>
算法笔记_056:蓝桥杯练习 未名湖边的烦恼(Java)
查看>>
idea的maven项目无法引入junit
查看>>
jquery实现限制textarea输入字数
查看>>
thinkphp5 csv格式导入导出(多数据处理)
查看>>
页面置换算法-LRU(Least Recently Used)c++实现
查看>>
如何获取Android系统时间是24小时制还是12小时制
查看>>
fur168.com 改成5917电影
查看>>
PHP上传RAR压缩包并解压目录
查看>>
codeforces global round 1题解搬运
查看>>
python os模块
查看>>