gpt4 book ai didi

ios - 方法混合实现未发生

转载 作者:行者123 更新时间:2023-11-29 03:45:54 24 4
gpt4 key购买 nike

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

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

最佳答案

您正在使用 class_getClassMethod 来获取实例方法的实现,您应该使用 class_getInstanceMethod 来代替。

method_exchangeImplementations 仍然以同样的方式使用

关于ios - 方法混合实现未发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com