Objective C類方法load和initialize的區別
阿新 • • 發佈:2018-12-25
1 +(void)load會引發+(void)initialize
2 /******* Interface *******/
3 @interface SuperClass : NSObject
4 @end
5
6 @interface ChildClass : SuperClass
7 @end
8
9 @interface Insideinitialize : NSObject
10 - (void)objectMethod;
11 @end
12
13 /******* Implementation *******/
14 @implementation SuperClass
15
16 + (void) initialize {
17 NSLog(@"%@ %s", [self class], __FUNCTION__);
18 }
19
20 + (void) load {
21 NSLog(@"%@ %s", [self class], __FUNCTION__);
22 }
23
24 @end
25
26 @implementation ChildClass
27
28 + (void) initialize {
29 NSLog(@"%@ %s", [self class], __FUNCTION__);
30 Insideinitialize * obj = [[Insideinitialize alloc] init];
31 [obj objectMethod];
32 [obj release];
33 }
34
35 @end
36
37 @implementation Insideinitialize
38
39 - (void)objectMethod {
40 NSLog(@"%@ %s", [self class], __FUNCTION__);
41 }
42
43 + (void) initialize {
44 NSLog(@"%@ %s", [self class], __FUNCTION__);
45 }
46
47 + (void) load {
48 NSLog(@"%s", __FUNCTION__);
49 }
50
51 @end