Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@ooopscc

This comment has been minimized.

Copy link

@ooopscc ooopscc 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