I am having a NSMutableDictionary
. I have to dynamically rename any Key
in the dictionary to a new value, in my code.. I can't find any built-in API to do this..
我有一个NSMutableDictionary。我必须在我的代码中动态地将字典中的任何键重命名为新值。我找不到任何内置API来执行此操作。
How can I do this? Is there any built-in API available to do this?
我怎样才能做到这一点?是否有可用的内置API?
Thanks everyone..
感谢大家..
5 个解决方案
#1
37
// 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"];
Think about what "directly editing an existing key" means. A dictionary is a hash; it hashes the contents of the keys to find a value.
想想“直接编辑现有密钥”的含义。字典是哈希;它散列键的内容以查找值。
What happens if you were to change the contents of a key? The key would need to be rehashed (and the dictionary's internal structures re-balanced) or the value would no longer be retrievable.
如果要更改密钥的内容会发生什么?密钥需要重新设置(字典的内部结构重新平衡)或者值不再可检索。
Why do you want to edit the contents of a key in the first place? I.e. what problem does that solve that the above does not?
为什么要首先编辑密钥的内容?即什么问题可以解决上面没有的问题?
#2
9
This should work:
这应该工作:
- (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];
}
This does exactly the same as bbum's answer but, if you remove the old key first (like in this example) then you have to retain the object temporarily otherwise it might get deallocated in the way ;)
这与bbum的答案完全相同,但是,如果你先删除旧密钥(就像在这个例子中那样),那么你必须暂时保留该对象,否则它可能会被解除分配;)
Conclusion: Unless you need explicitly to remove the old key first do as bbum.
结论:除非你需要明确删除旧密钥,否则首先要做为bbum。
#3
5
@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
This also handles the case where the dictionary doesn't have a value for the key nicely.
这也处理了字典没有很好地为键提供值的情况。
#5
0
I have to navigate a complete JSON response object that holds fields, sub-dictionaries and sub-arrays. All because one of the JSON fields is called "return" which is an iOS reserved word, so can't be used with the JSONModel Cocoa Pod. Here's the code:
我必须导航一个完整的JSON响应对象,该对象包含字段,子字典和子数组。全部是因为其中一个JSON字段被称为“返回”,这是一个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
}
#1
37
// 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"];
Think about what "directly editing an existing key" means. A dictionary is a hash; it hashes the contents of the keys to find a value.
想想“直接编辑现有密钥”的含义。字典是哈希;它散列键的内容以查找值。
What happens if you were to change the contents of a key? The key would need to be rehashed (and the dictionary's internal structures re-balanced) or the value would no longer be retrievable.
如果要更改密钥的内容会发生什么?密钥需要重新设置(字典的内部结构重新平衡)或者值不再可检索。
Why do you want to edit the contents of a key in the first place? I.e. what problem does that solve that the above does not?
为什么要首先编辑密钥的内容?即什么问题可以解决上面没有的问题?
#2
9
This should work:
这应该工作:
- (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];
}
This does exactly the same as bbum's answer but, if you remove the old key first (like in this example) then you have to retain the object temporarily otherwise it might get deallocated in the way ;)
这与bbum的答案完全相同,但是,如果你先删除旧密钥(就像在这个例子中那样),那么你必须暂时保留该对象,否则它可能会被解除分配;)
Conclusion: Unless you need explicitly to remove the old key first do as bbum.
结论:除非你需要明确删除旧密钥,否则首先要做为bbum。
#3
5
@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
This also handles the case where the dictionary doesn't have a value for the key nicely.
这也处理了字典没有很好地为键提供值的情况。
#4
#5
0
I have to navigate a complete JSON response object that holds fields, sub-dictionaries and sub-arrays. All because one of the JSON fields is called "return" which is an iOS reserved word, so can't be used with the JSONModel Cocoa Pod. Here's the code:
我必须导航一个完整的JSON响应对象,该对象包含字段,子字典和子数组。全部是因为其中一个JSON字段被称为“返回”,这是一个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
}