Skip to content

Commit 3a9d03f

Browse files
committed
更新demo
1 parent 95432ec commit 3a9d03f

8 files changed

Lines changed: 85 additions & 12 deletions

File tree

load/load.xcodeproj/project.pbxproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
9B8E43D31C5F6E78002ABEBC /* Parent.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B8E43D21C5F6E78002ABEBC /* Parent.m */; };
1212
9B8E43D61C5F6E7E002ABEBC /* Child.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B8E43D51C5F6E7E002ABEBC /* Child.m */; };
1313
9B8E43DC1C5F94E2002ABEBC /* Other.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B8E43DA1C5F94E2002ABEBC /* Other.m */; };
14+
9B8E43DF1C5FA5D5002ABEBC /* Child+load.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B8E43DE1C5FA5D5002ABEBC /* Child+load.m */; };
1415
/* End PBXBuildFile section */
1516

1617
/* Begin PBXCopyFilesBuildPhase section */
@@ -34,6 +35,8 @@
3435
9B8E43D51C5F6E7E002ABEBC /* Child.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Child.m; sourceTree = "<group>"; };
3536
9B8E43DA1C5F94E2002ABEBC /* Other.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Other.m; sourceTree = "<group>"; };
3637
9B8E43DB1C5F94E2002ABEBC /* Other.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Other.h; sourceTree = "<group>"; };
38+
9B8E43DD1C5FA5D5002ABEBC /* Child+load.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Child+load.h"; sourceTree = "<group>"; };
39+
9B8E43DE1C5FA5D5002ABEBC /* Child+load.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Child+load.m"; sourceTree = "<group>"; };
3740
/* End PBXFileReference section */
3841

3942
/* Begin PBXFrameworksBuildPhase section */
@@ -66,9 +69,11 @@
6669
9B8E43C91C5F6E40002ABEBC /* load */ = {
6770
isa = PBXGroup;
6871
children = (
69-
9B8E43DA1C5F94E2002ABEBC /* Other.m */,
70-
9B8E43DB1C5F94E2002ABEBC /* Other.h */,
7172
9B8E43CA1C5F6E40002ABEBC /* main.m */,
73+
9B8E43DD1C5FA5D5002ABEBC /* Child+load.h */,
74+
9B8E43DE1C5FA5D5002ABEBC /* Child+load.m */,
75+
9B8E43DB1C5F94E2002ABEBC /* Other.h */,
76+
9B8E43DA1C5F94E2002ABEBC /* Other.m */,
7277
9B8E43D11C5F6E78002ABEBC /* Parent.h */,
7378
9B8E43D21C5F6E78002ABEBC /* Parent.m */,
7479
9B8E43D41C5F6E7E002ABEBC /* Child.h */,
@@ -136,6 +141,7 @@
136141
9B8E43CB1C5F6E40002ABEBC /* main.m in Sources */,
137142
9B8E43D61C5F6E7E002ABEBC /* Child.m in Sources */,
138143
9B8E43D31C5F6E78002ABEBC /* Parent.m in Sources */,
144+
9B8E43DF1C5FA5D5002ABEBC /* Child+load.m in Sources */,
139145
9B8E43DC1C5F94E2002ABEBC /* Other.m in Sources */,
140146
);
141147
runOnlyForDeploymentPostprocessing = 0;

load/load/Child+load.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Child+load.h
3+
// load
4+
//
5+
// Created by 张星宇 on 16/2/1.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "Child.h"
10+
11+
@interface Child (load)
12+
13+
@end

load/load/Child+load.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Child+load.m
3+
// load
4+
//
5+
// Created by 张星宇 on 16/2/1.
6+
// Copyright © 2016年 zxy. All rights reserved.
7+
//
8+
9+
#import "Child+load.h"
10+
11+
@implementation Child (load)
12+
13+
+ (void)load {
14+
/**
15+
* 分类中的load方法会被调用
16+
*/
17+
NSLog(@"Load Class Child+load");
18+
}
19+
20+
/**
21+
* initialize更像是一个普通的方法。
22+
* 即使在Child.m中实现了initialize方法,也会在这里被覆盖
23+
*/
24+
//+ (void)initialize {
25+
// NSLog(@"Initialize Class Child+load");
26+
//}
27+
28+
@end

load/load/Child.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ @implementation Child
1414
+ (void)load {
1515
NSLog(@"Load Class Child");
1616

17-
Other *normal = [[Other alloc] init];
18-
NSLog(@"%@",normal);
17+
/**
18+
* 这时候Other类还没调用load方法,所以输出结果是Original Output
19+
*/
20+
Other *other = [Other new];
21+
[other originalFunc];
1922

20-
// 如果不先调用Normal的load,下面这行代码就无效,打印出null
23+
// 如果不先调用other的load,下面这行代码就无效,打印出null
2124
[Other printName];
2225
}
2326

load/load/Other.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
@interface Other : NSObject
1212

1313
+ (void)printName;
14+
- (void)originalFunc;
1415

1516
@end

load/load/Other.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,32 @@
77
//
88

99
#import "Other.h"
10+
#import <objc/runtime.h>
1011

1112
@implementation Other
1213

1314
static NSString *name;
1415

1516
+ (void)load {
1617
name = @"Other";
17-
NSLog(@"Load Class Normal");
18+
NSLog(@"Load Class Other");
19+
20+
Method originalFunc = class_getInstanceMethod([self class], @selector(originalFunc));
21+
Method swizzledFunc = class_getInstanceMethod([self class], @selector(swizzledFunc));
22+
23+
method_exchangeImplementations(originalFunc, swizzledFunc);
1824
}
1925

2026
+ (void)printName {
2127
NSLog(@"%@",name);
2228
}
2329

30+
- (void)originalFunc {
31+
NSLog(@"Original Output");
32+
}
33+
34+
- (void)swizzledFunc {
35+
NSLog(@"Swizzled Output");
36+
}
37+
2438
@end

load/load/Parent.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ + (void)load {
1717
NSLog(@"Load Class Parent");
1818
}
1919

20-
//+ (void)initialize {
21-
// NSLog(@"Initialize Parent, caller Class %@", [self class]);
22-
//}
20+
+ (void)initialize {
21+
NSLog(@"Initialize Parent, caller Class %@", [self class]);
22+
}
2323

2424
/**
2525
* 下面这个才是正确的initialize方法的实现,需要判断是不是由自己这个类调用的。

load/load/main.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88

99
#import <Foundation/Foundation.h>
1010
#import "Child.h"
11+
#import "Other.h"
1112

1213
int main(int argc, const char * argv[]) {
1314
@autoreleasepool {
14-
// insert code here...
15-
NSLog(@"Hello, World!");
16-
15+
NSLog(@"Main函数在这里开始");
16+
/**
17+
* 这会触发Child和父类的initialize方法
18+
*/
1719
Child *child = [Child new];
20+
21+
/**
22+
* 这时候other已经调用过load方法,所以输出结果是Swizzled Output
23+
*/
24+
Other *other = [Other new];
25+
[other originalFunc];
1826
}
1927
return 0;
2028
}

0 commit comments

Comments
 (0)