forked from bestswifter/MySampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodSwizzlingTest.m
More file actions
58 lines (44 loc) · 1.73 KB
/
MethodSwizzlingTest.m
File metadata and controls
58 lines (44 loc) · 1.73 KB
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
//
// MethodSwizzlingTest.m
// runtime
//
// Created by 张星宇 on 16/1/2.
// Copyright © 2016年 张星宇. All rights reserved.
//
#import "MethodSwizzlingTest.h"
#import "NSString+MyImplementation.h"
#import <objc/runtime.h>
/**
* 这个类用来演示Method Swizzle的原理。
*/
@interface MethodSwizzlingTest ()
- (void)startMethodSwizzling;
@end
static NSString *mixedString = @"Hello World";
@implementation MethodSwizzlingTest
/**
* 理论上来说,应该在load方法中线程安全地进行Method Swizzle,这里为了演示Method Swizzle可以动态进行,
* 我把startMethodSwizzling函数放在test方法中执行
*/
//+ (void)load {
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// Method originalLowercaseStringMethod = class_getInstanceMethod([NSString class], @selector(lowercaseString));
// Method newLowercaseStringMethod = class_getInstanceMethod([NSString class], @selector(myLowercaseString));
//
// method_exchangeImplementations(originalLowercaseStringMethod, newLowercaseStringMethod);
// });
//}
- (void)test {
NSLog(@"未进行Method Swizzling之前:");
NSLog(@"小写字符串:%@", [mixedString lowercaseString]);
[self startMethodSwizzling];
NSLog(@"进行Method Swizzling之后:");
NSLog(@"小写字符串:%@", [mixedString lowercaseString]);
}
- (void)startMethodSwizzling {
Method originalLowercaseStringMethod = class_getInstanceMethod([NSString class], @selector(lowercaseString));
Method newLowercaseStringMethod = class_getInstanceMethod([NSString class], @selector(myLowercaseString));
method_exchangeImplementations(originalLowercaseStringMethod, newLowercaseStringMethod);
}
@end