2011-02-02 191 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 
}