0

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

Parul
  • 25
  • 4
  • 2
    Apart from things not working, replacing nil values with @"Nil" is a very, very bad idea. [NSNull null] is meant to be used for exactly that purpose. – gnasher729 May 28 '16 at 12:16
  • Last time I came up with this kind of task, I followed through [Advice on how to catch “attempt to insert nil object” from a device needed](http://stackoverflow.com/a/23789405/1492173) SO answer. – Yevhen Dubinin May 28 '16 at 14:38

3 Answers3

7

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

change the code:

Class class = [self class];

to:

Class class = NSClassFromString(@"__NSDictionaryM");

worked for me,you can try it!

here is my whole code:

@implementation NSMutableDictionary (Category)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    [super load];
 Class class = NSClassFromString(@"__NSDictionaryM");
 SEL originalSelector = @selector(setObject:forKey:);
 SEL swizzledSelector = @selector(fu_setObject:forKey:);

    Method originalMethod = class_getInstanceMethod(class,originalSelector);
 Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
 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)fu_setObject:(id)anObject forKey:(id<NSCopying>)aKey{
   [self fu_setObject:anObject forKey:aKey];
   
}
@end
Princekin
  • 696
  • 9
  • 18
0

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98