无标题文章

NSDictionary+NilSafe.h 小神器

后台各种传,垃圾参数,各种空值,各种取空字典中的值,之前用空值来判断,但是太麻烦,而且不晓得具体是哪一个,所以后来找到一个神器。虽然有点暴力,但是能解决很打一部分的crash问题。

.h文件

@interface NSDictionary (NilSafe)

@end

@interface NSMutableDictionary (NilSafe)

@end


.m文件

#import  <objc/runtime.h>

#import "NSDictionary+NilSafe.h"

@implementation NSObject (Swizzling)

+ (BOOL)gl_swizzleMethod:(SEL)origSelwithMethod:(SEL)altSel {

    MethodorigMethod =class_getInstanceMethod(self, origSel);

    MethodaltMethod =class_getInstanceMethod(self, altSel);

    if(!origMethod || !altMethod) {

        returnNO;

    }

    class_addMethod(self,

                    origSel,

                    class_getMethodImplementation(self, origSel),

                    method_getTypeEncoding(origMethod));

    class_addMethod(self,

                    altSel,

                    class_getMethodImplementation(self, altSel),

                    method_getTypeEncoding(altMethod));

    method_exchangeImplementations(class_getInstanceMethod(self, origSel),

                                   class_getInstanceMethod(self, altSel));

    return YES;

}

+ (BOOL)gl_swizzleClassMethod:(SEL)origSel withMethod:(SEL)altSel {

    return[object_getClass((id)self)gl_swizzleMethod:origSelwithMethod:altSel];

}

@end

@implementation NSDictionary (NilSafe)

+ (void)load{

    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        [selfgl_swizzleMethod:@selector(initWithObjects:forKeys:count:)withMethod:@selector(gl_initWithObjects:forKeys:count:)];

        [selfgl_swizzleClassMethod:@selector(dictionaryWithObjects:forKeys:count:)withMethod:@selector(gl_dictionaryWithObjects:forKeys:count:)];

    });

}

+ (instancetype)gl_dictionaryWithObjects:(constid[])objectsforKeys:(constid [])keyscount:(NSUInteger)cnt {

    idsafeObjects[cnt];

    idsafeKeys[cnt];

    NSUIntegerj =0;

    for(NSUIntegeri =0; i < cnt; i++) {

        idkey = keys[i];

        idobj = objects[i];

        if(!key || !obj) {

            continue;

        }

        safeKeys[j] = key;

        safeObjects[j] = obj;

        j++;

    }

    return [self gl_dictionaryWithObjects:safeObjects forKeys:safeKeys count:j];

}

- (instancetype)gl_initWithObjects:(constid[])objectsforKeys:(constid [])keyscount:(NSUInteger)cnt {

    idsafeObjects[cnt];

    idsafeKeys[cnt];

    NSUIntegerj =0;

    for(NSUIntegeri =0; i < cnt; i++) {

        idkey = keys[i];

        idobj = objects[i];

        if(!key || !obj) {

            continue;

        }

        if(!obj) {

            obj = [NSNullnull];

        }

        safeKeys[j] = key;

        safeObjects[j] = obj;

        j++;

    }

    return[selfgl_initWithObjects:safeObjectsforKeys:safeKeyscount:j];

}

@end

@implementation NSMutableDictionary (NilSafe)

+ (void)load{

    staticdispatch_once_tonceToken;

    dispatch_once(&onceToken, ^{

        Classclass =NSClassFromString(@"__NSDictionaryM");

        [classgl_swizzleMethod:@selector(setObject:forKey:)withMethod:@selector(gl_setObject:forKey:)];

        [classgl_swizzleMethod:@selector(setObject:forKeyedSubscript:)withMethod:@selector(gl_setObject:forKeyedSubscript:)];

    });

}

- (void)gl_setObject:(id)anObjectforKey:(id)aKey {

    if(!aKey || !anObject) {

        return;

    }

    [selfgl_setObject:anObjectforKey:aKey];

}

- (void)gl_setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {

    if(!key || !obj) {

        return;

    }

    [self gl_setObject:obj forKeyedSubscript:key];

}

@end


摘自:https://github.com/allenhsu/NSDictionary-NilSafe

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【2017年最新】☞ iOS面试题及答案 设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经...
    紫色冰雨阅读 3,746评论 0 1
  • http://blog.csdn.net/david21984/article/details/57451917 ...
    紫色冰雨阅读 3,647评论 0 0
  • ``` +(BOOL)gl_swizzleMethod:(SEL)origSel withMethod:(SEL)...
    静水流深_436d阅读 1,046评论 0 0
  • [※]@property中有哪些属性关键字? 有3类。ARC下默认是(atomic, assign, readwr...
    Icyjade_White阅读 2,118评论 0 1
  • 本以为心如磐石,却终究人非草木。 爱则恨,念则忧。 据传,上古洪荒之时,西海有一岛,名愁绪,岛上有一草,生而有灵,...
    云柒妖孽阅读 4,189评论 0 2