[英]Method Swizzling Implementation not taking place
I am trying to learn the concept of swizzling. 我正在尝试学习下垂的概念。
Even though I have added method_exchangeImplementations
, still the methods are not being swizzled. 即使我添加了
method_exchangeImplementations
,这些方法仍然没有被淘汰。 Any idea as to where I am going wrong? 对我要去哪里错有任何想法吗?
#import <objc/runtime.h>
@interface POCViewController ()
- (void)addSwizzle;
- (void)originalMethod;
- (void)swizzledMethod;
@end
@implementation POCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Add swizzle
[self addSwizzle];
//Call the original method
[self originalMethod];
}
- (void)addSwizzle
{
Method original, swizz;
original = class_getClassMethod([self class], @selector(originalMethod));
swizz = class_getClassMethod([self class], @selector(swizzledMethod));
method_exchangeImplementations(original, swizz);
}
- (void)originalMethod
{
NSLog(@"Inside original method");
}
- (void)swizzledMethod
{
NSLog(@"Inside swizzled method");
[self swizzledMethod];
}
You are using class_getClassMethod
to get implementations of instance methods, you should use class_getInstanceMethod
instead. 您正在使用
class_getClassMethod
来获取实例方法的实现,而应该使用class_getInstanceMethod
。
method_exchangeImplementations
is still used the same way method_exchangeImplementations
仍以相同的方式使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.