好吧,所以我想从嵌套字典的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];
}
完美的,他们为什么不能像书中那样解释它,哈哈。在写完这篇文章后,我意识到我使用的是数组和字典,而不是使用词典的字典,但是你已经帮助清除了一些错误的理解,谢谢aqua。 – DIGGIDY 2011-01-16 10:15:09