-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.h
70 lines (64 loc) · 1.38 KB
/
Singleton.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Singleton.h
// recover
//
// Created by admin on 15/7/28.
// Copyright (c) 2015年 admin. All rights reserved.
////不加参数更为简单
#define SingletonH(MethodName) +(instancetype) shared##MethodName;
#if __has_feature(objc_arc)//编译环境是ARC
#define SingletonM(MethodName) \
static id _instance;\
+(id) allocWithZone:(struct _NSZone *)zone{\
if (_instance ==nil) {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
}\
return _instance;\
}\
-(id)init{\
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{\
_instance = [super init];\
});\
return _instance;\
}\
+(instancetype) shared##MethodName{\
return _instance = [[super alloc] init];\
}
#else //变异环境是非ARC
#define SingletonM(MethodName) \
static id _instance;\
+(id) allocWithZone:(struct _NSZone *)zone{\
if (_instance ==nil) {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
}\
return _instance;\
}\
-(id)init{\
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{\
_instance = [super init];\
});\
return _instance;\
}\
+(instancetype) shared##MethodName{\
return _instance = [[super alloc] init];\
}\
-(oneway void)release{\
}\
-(id)autorelease{\
return self;\
}\
-(id)retain{\
return self;\
}\
-(NSUInteger)retainCount{\
return 1;\
}
#endif