Object-C 语言合并两个字典

2017-01-12  本文已影响0人  nasy_iOS

- (id)mutableDictionaryCopyIfNeeded:(id)dictObj

{if([dictObj isKindOfClass:[NSDictionaryclass]] &&

![dictObj isKindOfClass:[NSMutableDictionaryclass]]) {

dictObj = [dictObj mutableCopy];

}returndictObj;

}

/**

合并两个字典

@param dict 被合并的字典

*/

- (void)mergingWithDictionary:(NSDictionary*)dict

{for(idkeyin[dict allKeys]) {idobj = [selfmutableDictionaryCopyIfNeeded:[dict objectForKey:key]];idlocalObj = [selfmutableDictionaryCopyIfNeeded:[selfobjectForKey:key]];if([obj isKindOfClass:[NSDictionaryclass]] &&

[localObj isKindOfClass:[NSMutableDictionaryclass]]) {// Recursive merge for NSDictionary[localObj mergingWithDictionary:obj];

}elseif(obj) {

[selfsetObject:obj forKey:key];

}

}

}/**

合并两个字典

@param dict      被合并的字典

@param ignoredKey 忽略的Key

*/- (void)mergingWithDictionary:(NSDictionary*)dict ignoredDictKey:(NSString*)ignoredKey

{for(idkeyin[dict allKeys]) {if([key isEqualToString:ignoredKey]) {continue;

}idobj = [selfmutableDictionaryCopyIfNeeded:[dict objectForKey:key]];idlocalObj = [selfmutableDictionaryCopyIfNeeded:[selfobjectForKey:key]];if([obj isKindOfClass:[NSDictionaryclass]] &&

[localObj isKindOfClass:[NSMutableDictionaryclass]]) {// Recursive merge for NSDictionary[localObj mergingWithDictionary:obj];

}elseif(obj) {

[selfsetObject:obj forKey:key];

}

}

}

上一篇下一篇

猜你喜欢

热点阅读