Vanilla swizzling:
SEL setObjectSelector = @selector(setObject:atIndex:);
[NSArray wml_replaceInstanceMethod:setObjectSelector withBlock:^(NSArray *self, id object, NSInteger index){
// your logic...
}];
Or if you want to call the original method from your new implementation:
__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);
}];
Restore the original implementation, once you're done with swizzling:
[WMLSwizzler replaceClassMethod:setObjectSelector inClass:[NSArray class] withImplementation:originalIMP];
If you're swizzling "the old" way, you might run into issues since you new selector xyz_swizzledMethod
might be passed when you call the original implementation. Besides, the block-based API is nicer and more concise.
Swizzling is all fun and giggles until you screw it up. Please refer to these articles for more info:
- A Story About Swizzling "the Right Way™" and Touch Forwarding
- The Right Way to Swizzle in Objective-C
Swizzler is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Swizzler"
Sash Zats, [email protected]
Swizzler is available under the MIT license. See the LICENSE file for more info.