2011-02-02 190 views
9

我有一个NSMutableDictionary。我在字典中的任何Key动态重命名为新的值,在我的代码。我找不到任何内置的API来做到这一点..如何在NSMutableDictionary中重命名密钥?

我怎样才能做到这一点?有没有内置的API可以做到这一点?

谢谢大家..

+0

它是复制的价值出来,然后创建具有所需名称的新钥匙的选项? – Jumhyn 2011-02-02 07:05:11

+0

我无法直接编辑现有的密钥? – EmptyStack 2011-02-02 07:07:29

回答

35
// 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"]; 

想想什么是 “直接编辑现有的密钥” 的意思。字典是一个散列;它会散列键的内容以查找值。

,如果你要改变一个关键的内容,会发生什么?密钥需要重新设置(并且字典的内部结构重新平衡),否则该值将不再可检索。

为什么你要编辑的一个关键的内容摆在首位?即这个问题解决了什么问题呢?

+0

谢谢..这很好..我不能直接编辑现有的密钥? – EmptyStack 2011-02-02 07:12:18

+0

嗯..没有什么具体..我只是期待一个直接的方式来编辑键..就是这样..谢谢..男人感谢 – EmptyStack 2011-02-02 07:30:24

+0

@bbum,这工作了NSUserdefault以及! – Douglas 2015-05-15 15:07:02

9

这应该工作:

- (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。

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 

这也处理这样的字典没有导航键的值很好的情况下。

0

我必须导航保存字段,子辞典和子阵列的完整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 
}