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

#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扩展(取值为空处理防止赋值崩溃))