2014-02-20 152 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 
    }]; 
}]; 

我没有测试它,但它应该给你一个想法。

相关问题