Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created October 23, 2013 19:27
Show Gist options
  • Save rcdilorenzo/7125050 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/7125050 to your computer and use it in GitHub Desktop.
Swizzling.
#import <Foundation/Foundation.h>
@interface NSObject (Swizzling)
- (void)swizzleMethod:(SEL)originalSelector toMethod:(SEL)newSelector;
@end
#import <objc/runtime.h>
@implementation NSObject (Swizzling)
- (void)swizzleMethod:(SEL)originalSelector toMethod:(SEL)newSelector {
Method origMethod = class_getInstanceMethod(self.class, originalSelector);
Method newMethod = class_getInstanceMethod(self.class, newSelector);
if(class_addMethod(self.class, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) {
class_replaceMethod(self.class, newSelector, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, newMethod);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment