Vanill
SEL setObjectSelector = @selector(setObject:atIndex:);
[NSArray wml_replaceInstanceMethod:setObjectSelector withBlock:^(NSArray *self, id object, NSInteger index){
// your logic...
}];
或者,如果您想要从您的新实现中调用原始方法
__block IMP originalIMP = [NSArray wml_replaceInstanceMethod:setObjectSelector withBlock:^(NSArray *self, id object, NSInteger index){
// hopefully this not what you're doing
originalIMP(self, setObjectSelector, object, self.count - index - 1);
}];
一旦您完成 swizzling,请恢复原始实现
[WMLSwizzler replaceClassMethod:setObjectSelector inClass:[NSArray class] withImplementation:originalIMP];
如果您以“旧方法”进行 swizzling,您可能会遇到一些问题,因为新的选择器 xyz_swizzledMethod
可能会在您调用原始实现时传递。此外,基于块的 API 更简单、更简洁。
Swizzling 一切都是有趣的,直到你搞砸了它。请参阅以下文章以获取更多信息
Swizzler 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "Swizzler"
Sash Zats, [email protected]
Swizzler 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。