Created
April 22, 2014 18:59
-
-
Save RuiAAPeres/11190505 to your computer and use it in GitHub Desktop.
Swizzling valueForKey: and objectForKey:
This file contains hidden or 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 NSDictionary (Swizzled) | |
static void swizzInstance(Class class, SEL originalSel, SEL newSel) | |
{ | |
Method origMethod = class_getInstanceMethod(class, originalSel); | |
Method newMethod = class_getInstanceMethod(class, newSel); | |
method_exchangeImplementations(origMethod, newMethod); | |
} | |
- (id)zzz_valueForKey:(NSString *)key | |
{ | |
return [self zzz_valueForKey:key]; | |
} | |
- (id)zzz_objectForKey:(NSString *)key | |
{ | |
return [self zzz_objectForKey:key]; | |
} | |
+ (void)swizz | |
{ | |
swizzInstance(NSClassFromString(@"__NSDictionaryM"),@selector(objectForKey:),@selector(zzz_objectForKey:)); | |
swizzInstance(NSClassFromString(@"__NSDictionaryM"),@selector(valueForKey:),@selector(zzz_valueForKey:)); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems that objectForKey: not working. I have tried for many times.