我正在尝试学习调酒的概念。
即使我已经添加了method_exchangeImplementations
,方法仍然没有被混合。关于我要去哪里错的任何想法?
#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];
}