【问题标题】:Swizzled method for NSMutableDictionary is not getting calledNSMutableDictionary 的 Swizzled 方法没有被调用
【发布时间】:2016-05-28 05:42:00
【问题描述】:

我正在尝试调配 NSMutableDictionary。我在这里做错了什么?我正在尝试为 NSMutableDictionary 覆盖 setObject:forKey:。

#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];
}

@结束

【问题讨论】:

标签: ios objective-c nsmutabledictionary swizzling method-swizzling


【解决方案1】:

NSMutableDictionary 是一个class cluster。它是许多实现实际工作的 Apple 私有具体子类的抽象基类。作为子类,它们覆盖-setObject:forKey:NSMutableDictionary 本身并没有真正实现该方法,也没有任何尝试调用它。因此,没有什么可以调用您的替代实现。

【讨论】:

    【解决方案2】:

    在被告知为什么它不起作用之后:只需向 NSMutableDictionary 添加一个新方法,例如 -(void)setObject:(id)object 或 NullForKey:(NSString*)key。

    如果对象为 nil,则不尝试添加甚至删除对象的方法可能更有用。这样,objectForKey 将返回 nil,与您传入的相同。

    【讨论】:

      【解决方案3】:

      修改代码:

      Class class = [self class];

      到:

      Class class = NSClassFromString(@"__NSDictionaryM");

      为我工作,你可以试试!

      这是我的全部代码:

      @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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 2012-03-13
        • 1970-01-01
        • 2014-06-02
        • 2011-08-28
        • 2017-02-24
        • 2015-02-23
        相关资源
        最近更新 更多