我有一个
NSMutableDictionary
,我需要在我的代码中将字典中的任何 Key
动态重命名为新值。 我找不到任何内置的API来完成这个操作。如何实现?是否有可用的内置API?谢谢大家。NSMutableDictionary
,我需要在我的代码中将字典中的任何 Key
动态重命名为新值。 我找不到任何内置的API来完成这个操作。如何实现?是否有可用的内置API?谢谢大家。// assumes that olkdey and newkey won't be the same; they can't as
// constants... but...
[dict setObject: [dict objectForKey: @"oldkey"] forKey: @"newkey"];
[dict removeObjectForKey: @"oldkey"];
想一下"直接编辑现有键"是什么意思。字典是一个哈希表;它将键的内容进行哈希以查找值。
如果您更改键的内容会发生什么?该键需要重新哈希(并且需要重新平衡字典的内部结构),否则该值将无法检索。
首先,为什么要编辑键的内容?也就是说,这解决了什么问题,而上述方法没有解决?
oldkey
等于 newkey
,则此操作将失败。 - de.- (void) renameKey:(id<NSCopying>)oldKey toKey:(id<NSCopying>)newKey{
NSObject *object = [dictionary objectForKey:oldKey];
[object retain];
[dictionary removeObjectForKey:oldKey];
[dictionary setObject:object forKey:newKey];
[object release];
}
这与bbum的答案完全相同,但是,如果您首先删除旧密钥(就像在此示例中一样),则必须暂时保留对象,否则可能会被释放掉。
结论:除非您明确需要先删除旧密钥,否则请按照bbum的方法操作。
@interface NSMutableDictionary (KAKeyRenaming)
- (void)ka_replaceKey:(id)oldKey withKey:(id)newKey;
@end
@implementation NSMutableDictionary (KAKeyRenaming)
- (void)ka_replaceKey:(id)oldKey withKey:(id)newKey
{
id value = [self objectForKey:oldKey];
if (value) {
[self setObject:value forKey:newKey];
[self removeObjectForKey:oldKey];
}
}
@end
我必须遍历一个完整的JSON响应对象,其中包含字段、子字典和子数组。这是因为JSON字段之一被称为“return”,它是iOS保留字,因此不能与JSONModel Cocoa Pod一起使用。 以下是代码:
+ (id) sanitizeJSON:(id) dictIn {
if (dictIn) //check not null
{
// if it's a dictionary item
if ([dictIn isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *dictOut = [dictIn mutableCopy];
// Do the fix replace "return" with "not_return"
if ([dictOut objectForKey: @"return"])
{[dictOut setObject: [dictIn objectForKey: @"return"] forKey: @"not_return"];
[dictOut removeObjectForKey: @"return"];}
// Continue the recursive walk through
NSArray*keys=[dictOut allKeys]; //get all the keys
for (int n=0;n<keys.count;n++)
{
NSString *key = [keys objectAtIndex:n];
//NSLog(@"key=%@ value=%@", key, [dictOut objectForKey:key]);
if (([[dictOut objectForKey:key] isKindOfClass:[NSDictionary class]]) || ([[dictOut objectForKey:key] isKindOfClass:[NSArray class]]))
{
// recursive call
id sanitizedObject = [self sanitizeJSON:[dictOut objectForKey:key]];
[dictOut removeObjectForKey: key];
[dictOut setObject:sanitizedObject forKey:key];
// replace returned (poss modified) item with this one
}
}
return dictOut; //return dict
}
else if ([dictIn isKindOfClass:[NSArray class]]) //Or if it's an array item
{
NSMutableArray *tempArray = [dictIn mutableCopy];
// Do the recursive walk across the array
for (int n=0;n< tempArray.count; n++)
{
// if array item is dictionary
if (([[tempArray objectAtIndex:n] isKindOfClass:[NSDictionary class]]) || ([[tempArray objectAtIndex:n] isKindOfClass:[NSArray class]]))
{
// recursive call
id sanitizedObject = [self sanitizeJSON:[tempArray objectAtIndex:n]];
// replace with the possibly modified item
[tempArray replaceObjectAtIndex:n withObject:sanitizedObject];
}
}
return tempArray; //return array
}
return dictIn; //Not nil or dict or array
}
else
return dictIn; //return nil
}