Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Created April 22, 2014 18:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuiAAPeres/11190505 to your computer and use it in GitHub Desktop.
Save RuiAAPeres/11190505 to your computer and use it in GitHub Desktop.
Swizzling valueForKey: and objectForKey:
#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
@yam-liu
Copy link

yam-liu commented May 10, 2014

It seems that objectForKey: not working. I have tried for many times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment