gpt4 book ai didi

ios - 未调用 NSMutableDictionary 的 Swizzled 方法

转载 作者:行者123 更新时间:2023-11-28 18:09:09 24 4
gpt4 key购买 nike

我正在尝试调配 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];
}

@结束

最佳答案

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

关于ios - 未调用 NSMutableDictionary 的 Swizzled 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37495628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com