オクラホマのネストされた辞書の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];
}
パーフェクト、なぜ彼らは本のようにそれを説明することはできません、笑。これを書いた後、私は辞書付きで辞書を使っていたが、辞書で辞書を使っていなかったということを理解しましたが、いくつかの誤解を覚えました。 – DIGGIDY