-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle.h
More file actions
43 lines (39 loc) · 772 Bytes
/
Single.h
File metadata and controls
43 lines (39 loc) · 772 Bytes
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
//
// Single.h
// test
//
// Created by 周超 on 2016/12/10.
// Copyright © 2016年 周超. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Single : NSObject
#define SingleH(name) +(instancetype)shared##name;
#define SingleM(name) static id P=nil;\
\
+(void)load{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
P=[[self alloc]init];\
});\
}\
\
+(instancetype)shared##name{\
return P;\
}\
\
+(instancetype)alloc{\
if (P) {\
NSException * excp=[NSException exceptionWithName:@"这是一个单例" reason:@"单例不允许创建多个实例,请用+(instancetype)shared类名 方法获取单例" userInfo:nil];\
[excp raise];\
}\
return [super alloc];\
}\
\
-(id)copy{\
return P;\
}\
\
-(id)mutableCopy{\
return P;\
}\
@end