iOS单例宏
// // Single.h // FKLTest // // Created by kun on 16/8/11. // Copyright © 2016年 kun. All rights reserved. // #ifndef Single_h #define Single_h #define SingelH(name) +(instancetype)share##name; #if __has_feature(objc_arc) // 条件编译 ARC #define SingelM(name) static id share##name = nil;\ + (instancetype)allocWithZone:(struct _NSZone *)zone\ {\ static dispatch_once_t onceToken;\ dispatch_once(&onceToken, ^{\ if ( !share##name )\ {\ share##name = [super allocWithZone:zone];\ }\ });\ return share##name;\ }\ + (instancetype)share##name\ {\ return [[self alloc] init];\ }\ - (id)copyWithZone:(NSZone *)zone\ {\ return share##name;\ }\ - (id)mutableCopyWithZone:(NSZone *)zone\ {\ return share##name;\ } #else // 条件编译 MRC #define SingelM(name) static id share##name = nil;\ + (instancetype)allocWithZone:(struct _NSZone *)zone\ {\ static dispatch_once_t onceToken;\ dispatch_once(&onceToken, ^{\ if ( !share##name )\ {\ share##name = [super allocWithZone:zone];\ }\ });\ return share##name;\ }\ + (instancetype)share##name\ {\ return [[self alloc] init];\ }\ - (id)copyWithZone:(NSZone *)zone\ {\ return share##name;\ }\ - (id)mutableCopyWithZone:(NSZone *)zone\ {\ return share##name;\ }\ - (oneway void)release\ {\ }\ - (instancetype)retain\ {\ return shared##name;\ }\ - (NSUInteger)retainCount\ {\ return MAXFLOAT;\ } #endif // 条件编译 #endif /* Single_h */
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步