美文网首页
iOS NSDictionary扩展(取值为空处理防止赋值崩溃)

iOS NSDictionary扩展(取值为空处理防止赋值崩溃)

作者: xh_0129 | 来源:发表于2016-10-14 15:13 被阅读0次

    #import@interface NSDictionary (VASafeObjectForKey)

    -(id)safeObjectForKey:(NSString*) key;

    -(NSMutableDictionary *)mutableDeepCopy;

    @end

    #import "NSDictionary+VASafeObjectForKey.h"

    @implementation NSDictionary (VASafeObjectForKey)

    -(id)safeObjectForKey:(NSString*) key

    {

    id object = [self objectForKey:key];

    if ([object isKindOfClass:[NSNull class]]) {

    object = nil;

    }

    return object;

    }

    -(NSMutableDictionary *)mutableDeepCopy{

    NSMutableDictionary *copyDict = [[NSMutableDictionary alloc]initWithCapacity:self.count];

    for (id key in self.allKeys) {

    id oneCopy = nil;

    id oneValue = [self valueForKey:key];

    if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]) {

    oneCopy = [oneValue mutableDeepCopy];

    //        }else if ([object respondsToSelector:@selector(mutableCopy)]){

    //            oneCopy = [object mutableCopy];

    }else if ([oneValue respondsToSelector:@selector(copy)]){

    oneCopy = [oneValue copy];

    }

    [copyDict setValue:oneCopy forKey:key];

    }

    return copyDict;

    }

    @end

    相关文章

      网友评论

          本文标题:iOS NSDictionary扩展(取值为空处理防止赋值崩溃)

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