-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFInitializing.h
181 lines (150 loc) · 4.96 KB
/
RFInitializing.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*!
RFInitializing
Stop writing init methods again and again.
Copyright (c) 2013-2014, 2018 BB9z
https://github.com/RFUI/RFInitializing
The MIT License (MIT)
http://www.opensource.org/licenses/mit-license.php
*/
#import <Foundation/Foundation.h>
/**
## Purpose
It´s boring to write the init method again and again, especially there are
many init mehods to overwrite. For example, if you want subclass UIView,
you may overwrite ini, initWithFrame:, initWithColder:. And if you want
subclass that class you also should overwrite these methods again, WTF.
It´s time to end these meaningless repetition. By conforms to
RFInitializing, you can only write these init method in root class once,
then in subclass you can only implement `onInit` and `afterInit`, no more
init.
Attention, if a class conforms to RFInitializing, `onInit` should be called
during init and before init method return. But `afterInit` should called
after the method finished which init was called in it usually. eg:
@code
- (void)viewDidLoad {
[super viewDidLoad];
// RFButton conforms to RFInitializing.
RFButton *button = [[RFButton alloc] init];
// `onInit` was called before here.
// Do some config.
button.icon = [UIImage imageNamed:@"pic"];
// Any other code.
// `afterInit` won't be called in this scope.
}
// `afterInit` will be called after viewDidLoad executed in this example.
@endcode
## Usage
You should only call `onInit` and `afterInit` in root object which conforms
to this protocol. And `afterInit` must be delayed. Here is a example:
@code
- (id)init {
self = [super init];
if (self) {
[self onInit];
// Delay execute afterInit, you can also use GCD.
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];
}
return self;
}
@endcode
In subclass, you must not call these method in init method. You may
implemente onInit or afterInit for customize. And you should call super at
some point in your implementation if you override onInit or afterInit. eg:
@code
// If you had to add another init method.
- (instancetype)initWithSomething:(id)some {
self = [super init];
if (self) {
// Don't call onInit or afterInit.
self.something = some;
}
return self;
}
- (void)onInit {
[super onInit];
// Something
}
- (void)afterInit {
[super afterInit];
// Something
}
@endcode
*/
/**
Checkout https://github.com/RFUI/RFInitializing for usage.
*/
@protocol RFInitializing
@required
- (void)onInit;
- (void)afterInit;
@end
#ifndef RFInitializingRootForNSObject
#define RFInitializingRootForNSObject \
- (instancetype)init {\
self = [super init];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];\
}\
return self;\
}
#endif
#ifndef RFInitializingRootForNSObjectSupportNSCoding
#define RFInitializingRootForNSObjectSupportNSCoding \
- (instancetype)init {\
self = [super init];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];\
}\
return self;\
}\
- (instancetype)initWithCoder:(NSCoder *)aDecoder {\
self = [super initWithCoder:aDecoder];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];\
}\
return self;\
}
#endif
/// UIView’s init method will call initWithFrame:, so leave it empty.
#ifndef RFInitializingRootForUIView
#define RFInitializingRootForUIView \
- (instancetype)initWithFrame:(CGRect)frame {\
self = [super initWithFrame:frame];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];\
}\
return self;\
}\
- (instancetype)initWithCoder:(NSCoder *)aDecoder {\
self = [super initWithCoder:aDecoder];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:self afterDelay:0];\
}\
return self;\
}
#endif
/// UIViewController’s init method will call initWithNibName:bundle:.
#ifndef RFInitializingRootForUIViewController
#define RFInitializingRootForUIViewController \
- (instancetype)initWithCoder:(NSCoder *)aDecoder {\
self = [super initWithCoder:aDecoder];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:nil afterDelay:0];\
}\
return self;\
}\
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {\
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];\
if (self) {\
[self onInit];\
[self performSelector:@selector(afterInit) withObject:nil afterDelay:0];\
}\
return self;\
}
#endif