首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >方法Swizzling实现未发生

方法Swizzling实现未发生
EN

Stack Overflow用户
提问于 2013-07-20 20:41:10
回答 1查看 498关注 0票数 0

我正在尝试学习swizzling的概念。

即使我已经添加了method_exchangeImplementations,这些方法仍然没有被搅乱。你知道我哪里错了吗?

代码语言:javascript
复制
#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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-20 20:48:54

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

method_exchangeImplementations仍然以相同的方式使用

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17762374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档