Object-C 语言合并两个字典
- (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];
}
}
}