问题
I'm trying to swizzle NSMutableDictionary. What am I doing wrong here? I'm trying to override setObject:forKey: for NSMutableDictionary.
#import "NSMutableDictionary+NilHandled.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (NilHandled)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(setObject:forKey:);
SEL swizzledSelector = @selector(swizzledSetObject:forKey:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
class_replaceMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
BOOL didAddMethod = class_addMethod(class,originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void) swizzledSetObject:(id)anObject forKey:(id<NSCopying>)aKey
{
[self swizzledSetObject:anObject?anObject:@"Nil" forKey:aKey];
}
@end
回答1:
NSMutableDictionary
is a class cluster. It is an abstract base class for a number of Apple-private concrete subclasses that implement the real work. As subclasses, they override -setObject:forKey:
. There is no real implementation of that method on NSMutableDictionary
itself and nothing attempts to call it. Therefore, nothing calls your replacement implementation.
回答2:
After being told why it doesn't work: Just add a new method to NSMutableDictionary like -(void)setObject:(id)object orNullForKey:(NSString*)key.
Possibly more useful is a method that doesn't try to add, or even removes, the object if it is nil. That way, objectForKey will return nil, the same that you passed in.
来源:https://stackoverflow.com/questions/37495628/swizzled-method-for-nsmutabledictionary-is-not-getting-called