gpt4 book ai didi

ios - Swizzling - 在 Objective-C iOS 中撤消方法选择器 swizzling

转载 作者:行者123 更新时间:2023-11-28 21:46:40 26 4
gpt4 key购买 nike

我们在 Objective-C 中使用 Swizzling。为方便起见,我们使用 CoconutKit 的辅助方法来实现此目的:

HLSSwizzleSelectorWithBlock_Begin([TMObjectCache class], @selector(objectForKey:))
^(TMObjectCache *self, NSString *key) {
key = nil;
return ((id (*)(id, SEL, NSString*))_imp)(self, _cmd, key);

}
HLSSwizzleSelectorWithBlock_End;

不过,我们还想在代码中的某个位置之后取消调配此方法。有办法做到这一点吗?

最佳答案

我不熟悉 CoconutKit,但 swizzling 只是对 method_exchangeImplementations() 的调用。该函数交换两个实现。因此,如果您使用相同的参数再次调用它,您将把实现交换回来。您需要查看 HLSSwizzleSelectorWithBlock_Begin 如何构建对 method_exchangeImplementations() 的调用并再次调用。

当然,插入所有标准警告,即 swizzling 非常危险、脆弱,主要用于调试,如果有任何其他可能的解决方案,应强烈避免在生产代码中使用。

关于ios - Swizzling - 在 Objective-C iOS 中撤消方法选择器 swizzling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899133/

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