Created
October 23, 2013 19:27
-
-
Save rcdilorenzo/7125050 to your computer and use it in GitHub Desktop.
Swizzling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSObject (Swizzling) | |
- (void)swizzleMethod:(SEL)originalSelector toMethod:(SEL)newSelector; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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