0

我正在尝试学习调酒的概念。

即使我已经添加了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];
}
4

1 回答 1

1

您正在使用class_getClassMethod获取实例方法的实现,您应该使用它class_getInstanceMethod

method_exchangeImplementations仍然以同样的方式使用

于 2013-07-20T12:48:54.070 回答