美文网首页
利用runtime的方式进行判断为空的时候崩溃的问题

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

作者: guoguojianshu | 来源:发表于2018-09-15 19:31 被阅读5次

#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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/dfutnftx.html