2014-02-20 120 views
-4

我有一個包含項目數組(NSDictionary類型)的NSMutableDictionary。我需要刪除存儲在每個項目中的一個特定鍵(及其關聯的對象)。什麼是最好的方式去做這件事?如何從嵌套的不可變集合中移除項目

實施例結構:

NSDictionary *dict = @{ 
     @"v" : @[ 
      @{ @"key1": @"abc", @"key2" : @"def" }, 
      @{ @"key1": @"ghi", @"key2" : @"jkl" }, ... 
     ] 
    }; 

欲消除來自嵌套字典元件所有KEY1:

@{ 
    @"v" : @[ 
     @{ @"key2" : @"def" }, 
     @{ @"key2" : @"jkl" }, ... 
    ] 
} 
+0

你的意思是你有一個字典,你有數組對象,每個數組有更多的字典? – Merlevede

+0

是什麼讓你的數組成爲字典? –

+0

@Merlevede是的,你是對的。 – Boon

回答

2

這應做到:

NSMutableDictionary *dict = [@{ 
         @"v" : @[ 
           @{ @"key1": @"abc", @"key2" : @"def" }, 
           @{ @"key1": @"ghi", @"key2" : @"jkl" } 
           ] 
         } mutableCopy]; 

NSArray *oldArray = [dict objectForKey:@"v"]; 
NSMutableArray *newArray = [NSMutableArray array]; 
[oldArray enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger idx, BOOL *stop) { 
    NSMutableDictionary *mutableDictionary = [dictionary mutableCopy]; 
    [mutableDictionary removeObjectForKey:@"key1"]; 
    [newArray addObject:mutableDictionary]; 
}]; 
[dict setObject:newArray forKey:@"v"]; 
+0

爲什麼'__block'聲明'newArray'時? – Sebastian

+0

@Sebastian通常會阻止「複製」它們內部的變量,因此變量的更改在塊外部不可用。見https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html#//apple_ref/doc/uid/TP40007502-CH6-SW6 –

+0

(你不需要' __block',如果你使用了簡單的for循環而不是'enumerateObjectsUsingBlock:'。 –

-1

枚舉通過NSArray和呼叫[dict removeObjectForKey:@"key1"]對於每個字典。

編輯: 如果他們的NSDictionary代替的NSMutableDictionary,請執行下列操作:

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init]; 
[mutableDict setDictionary:originalDict]; 
[mutableDict removeObjectForKey:@"key1"]; 
NSDictionary *newDict = [[NSDictionary alloc] initWithDictionary:mutableDict]; 

使用newDict,但是你會喜歡的,可能取代的NSArray

編輯原來的再次整個解決方案:

NSArray *a = [outerDict objectForKey:@"v"]; 
NSMutableArray *newArray = [[NSMutableArray alloc] init]; 
for (NSDictionary d in a) { 
    NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init]; 
    [mutableDict setDictionary:d]; 
    [mutableDict removeObjectForKey:@"key1"]; 
    NSDictionary *newDict = [[NSDictionary alloc] initWithDictionary:mutableDict]; 
    [newArray addObject:newDict]; 
} 
[outerDict setObject:newArray ForKey:@"v"]; 
+0

當NSArray存儲NSDictionary時,如何調用removeObjectForKey? – Boon

+0

如果它們是'NSDictionary's,那麼將它們複製到NSMutableDictionary中,然後調用remove。恐怕沒有快速有效的方法來做到這一點,因爲NSDictionary是不可變的。 – Simon

+0

我不認爲你的解決方案將工作,因爲key1是在數組內的字典。 – Boon

-1

假設內部字典實際上是可變的(如你所述),而不是在線,你想是這樣的:

NSString *removeKey = @"key1"; 
for (NSArray *array in [outerDict allValues]) 
{ 
    for (NSMutableDictionary *dict in arrayOfDicts) 
    { 
     [dict removeObjectForKey:removeKey]; 
    } 
} 

...但你仍然需要有可變的內部字典。如果你想設置它內聯,並將它們是可變的,這將工作:

NSDictionary *dict = @{ 
         @"v" : @[ 
           [@{ @"key1": @"abc", @"key2" : @"def" } mutableCopy], 
           [@{ @"key1": @"ghi", @"key2" : @"jkl" } mutableCopy] 
           ] 
         }; 
+0

你不需要一個關鍵的最外層。這是字典文字。 – Boon

0

爲了您的主詞典,你可以使用enumerateKeysAndObjectsUsingBlock:列舉你的對象和對於數組,你可以使用enumerateObjectsUsingBlock

[self.myDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 
    NSArray *array = (NSArray *)obj; 
    [arr enumerateObjectsUsingBlock:^(id obj2, NSUInteger idx, BOOL *stop) 
    { 
     NSDictionary *dict = (NSDictonary *)obj2; 
     // remove item from dict 
    }]; 
}]; 

我沒有測試它,但它應該給你一個想法。

相關問題