利用runtime的方式进行判断为空的时候崩溃的问题

2018-09-15  本文已影响5人  guoguojianshu

#import

@interfaceNSMutableDictionary (objc)

@end

#import "NSMutableDictionary+objc.h"

#import

@implementationNSMutableDictionary (objc)

+ (void)load {

//这个是类簇,实际的字典,这个必须得用这个形式,使用self不行

    Class dictCls = NSClassFromString(@"__NSDictionaryM");

    MethodoriginalMethod =class_getInstanceMethod(dictCls,@selector(setObject:forKey:));

    MethodswizzledMethod =class_getInstanceMethod(dictCls,@selector(na_setObject:forKey:));

    method_exchangeImplementations(originalMethod, swizzledMethod);

}

- (void)na_setObject:(id)anObject forKey:(id)aKey {

    if(!anObject)

        return;

    [selfna_setObject:anObjectforKey:aKey];

}

@end

上一篇 下一篇

猜你喜欢

热点阅读