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 */
复制代码

 

posted on   FKunLam  阅读(132)  评论(0编辑  收藏  举报

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示