好吧,所以我想從嵌套字典的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