2011-01-13 66 views
1

好吧,所以我想从嵌套字典的NSDictionary创建一个临时NSDictionary,但我想从顶级字典深层复制单个项目(字典)。如何从NSDictionary复制个别嵌套NSDictionary的临时NSMutableDictionary

最终的结果是有一个过滤字典,我可以处理和而不影响主词典丢弃到。

听起来很混乱,所以怎么样一些代码来告诉你我是什么意思,继承人我工作的功能,这是一个粗略的编码布局,但在其过程中的路径基本完成。

我已经看过参考书和各种样品在线没有喜悦。

干杯, 达伦

- (void)setPricingData 
{ 
    // get selected lens option 
    NSDictionary *aOption = [self.lensOptionsDict objectAtIndex:self._lensOptionsIndex]; 
    if (aOption == nil) 
     return; 

    // get selected lens type 
    NSDictionary *aType = [self.lensTypesDict objectAtIndex:self._lensTypesIndex]; 
    if (aType == nil) 
     return; 

    // get lens option id and variation_id 
    NSString *option_id = [aOption valueForKey:@"id"]; 
    NSString *option_variation_id = [aOption valueForKey:@"variation_id"]; 

    // create temp dictionary for type pricing selection 
    int count = [self.lensTypesDict count]; 
    NSMutableDictionary *aPrices = [[NSMutableDictionary alloc] initWithCapacity:count]; 

    // cycle prices for option id and variation_id matches 
    for (NSDictionary *item in self.pricesDict) 
    { 
     NSString *variation_id = [item valueForKey:@"variation_id"]; 
     NSString *value_id = [item valueForKey:@"value_id"]; 

     // add matches to temp dictionary 
     if ([option_variation_id isEqualToString: variation_id]) 
     { 
      if ([option_id isEqualToString: value_id]) 
       [aPrices addObject: item]; 
     } 
    } 

    // get price from temp dictionary for selected lens type index 
    NSDictionary *price = [aPrices objectAtIndex:self._lensTypesIndex]; 
    if (price != nil) 
    { 
     // assign values to outlet 
     self.priceAndStockId = [price valueForKey:@"price"]; 
     self.priceSelected = [price valueForKey:@"price"]; 
    } 

    // release temp dictionary 
    [aPrices release]; 
} 

回答

1

它看起来像你使用数组混合了字典。

阵列,以应对objectAtIndex,而词典来objectForKeys回应。请记住,数组是一组可以编入索引的单元格,从0开始一直到[array count] - 1

除了使用散列函数作为索引方法外,字典与数组类似。这意味着你需要一个键来获取,设置和反对。

设置对象在NSMutableDictionary

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init]; 
[myDictionary setObject:anObject forKey:aKey]; 

或者,你可以有钥匙和对象的对应数组的数组,然后执行:

NSDictionary *completeDictionary; 
completeDictionary = [NSDictionary dictionaryWithObjects:objectArray 
         forkeys:keyArray count:[keyArray count]]; 

在这两种情况下,你必须有对象的键。这是相对于常规的数组中,你可以简单地做

[myArray addObject:myObject]; 

要想从一个字典对象,做

myObject = [myDictionary objectForKey:key]; 

从阵列获取的对象,做

myObject = [myArray objectAtIndex:anIntegerIndex]; 

最后,您的原始问题涉及深拷贝。要使您的字典保留不会更改的对象,即深度复制,您可以执行以下操作:

假设我想在字典中存储字典,并且我有一个关联的顶级字典键,级字典,我可以做到以下几点:

我有一个NSMutableDictionary,称为topLevelDictionary 我有一个NSDictionary,称为dictionaryTwo 我有一个NSString,这是我的钥匙,叫myKey

为了dictionaryTwo的深层副本,我可以做

// assuming topLevelDictionary is previously defined 
[topLevelDictionary setObject:[[dictionaryTwo copy] autorelease] forKey:myKey]; 

这样topLevelDictionary将包含dictionaryTwo副本,由此,如果dictionaryTwo变化,topLevelDictionary对象不会。

+0

完美的,他们为什么不能像书中那样解释它,哈哈。在写完这篇文章后,我意识到我使用的是数组和字典,而不是使用词典的字典,但是你已经帮助清除了一些错误的理解,谢谢aqua。 – DIGGIDY 2011-01-16 10:15:09