Last active
June 13, 2019 20:06
-
-
Save CS2Us/93b39c7eef448c54352139c2fe88b22c to your computer and use it in GitHub Desktop.
Objective-C Method Swizzle 使用过程中需要注意的问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// testDispatch | |
// | |
// Created by guoyiyuan on 2019/6/6. | |
// Copyright © 2019 guoyiyuan. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
@interface Father : NSObject | |
-(void)work; | |
@end | |
@implementation Father | |
-(void)work{ | |
NSLog(@"父亲得赚钱养家"); | |
} | |
@end | |
@interface Son : Father | |
@end | |
@implementation Son | |
BOOL simple_Swizzle(Class aClass, SEL originalSel,SEL swizzleSel){ | |
Method originalMethod = class_getInstanceMethod(aClass, originalSel); | |
Method swizzleMethod = class_getInstanceMethod(aClass, swizzleSel); | |
method_exchangeImplementations(originalMethod, swizzleMethod); | |
return YES; | |
} | |
BOOL best_Swizzle(Class aClass, SEL originalSel,SEL swizzleSel){ | |
Method originalMethod = class_getInstanceMethod(aClass, originalSel); | |
Method swizzleMethod = class_getInstanceMethod(aClass, swizzleSel); | |
BOOL didAddMethod = class_addMethod(aClass, originalSel, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod)); | |
if (didAddMethod) { | |
class_replaceMethod(aClass, swizzleSel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); | |
}else{ | |
method_exchangeImplementations(originalMethod, swizzleMethod); | |
} | |
return YES; | |
} | |
@end |
添加以下代码,结果会是怎样呢?
@implementation Son (Swizzle_another)
+(void)load {
NSLog(@"Swizzle_another 被调用");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
simple_Swizzle([self class], NSSelectorFromString(@"work"), @selector(son_another_work));
});
}
-(void)son_another_work {
[self son_work];
NSLog(@"son分类里的son_another_work");
}
@end
@implementation Son (Swizzle)
-(void)son_work{
[self son_work];
NSLog(@"son分类里的son_work");
}
+(void)load{
NSLog(@"Swizzle 被调用");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
simple_Swizzle([self class], NSSelectorFromString(@"work"), @selector(son_work));
});
}
@end
int main() {
Son *son = [Son new];
[son work];
}
添加以下代码,结果会是怎样呢?
@implementation Son (Swizzle)
-(void)son_work{
[self son_work];
NSLog(@"son分类里的son_work");
}
+(void)load{
NSLog(@"Swizzle 被调用");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
simple_Swizzle([self class], NSSelectorFromString(@"work"), @selector(son_work));
});
}
@end
@implementation Son (Swizzle_another)
+(void)load {
NSLog(@"Swizzle_another 被调用");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
simple_Swizzle([self class], NSSelectorFromString(@"work"), @selector(son_work));
});
}
-(void)son_work {
[self son_work];
NSLog(@"son分类里的son_another_work");
}
@end
int main() {
Son *son = [Son new];
[son work];
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
添加如下代码,结果会是怎样呢?
修改上面程序:
导致的
son_work
递归调用问题如何解决: