2011-01-13 6 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]]; 

をどちらの場合も、あなたは必見オブジェクトのためのキーを持っています。これは単純に、辞書からオブジェクトを取得配列からオブジェクトを取得するには

myObject = [myDictionary objectForKey:key]; 

を行い、

myObject = [myArray objectAtIndex:anIntegerIndex]; 
を行うには

[myArray addObject:myObject]; 

を行うことが可能な通常の配列とは対照的です。

最後に、元の質問はディープコピーに関連していました。辞書が変更されないオブジェクト、つまりディープコピーを保持するようにするには、次のようにします。

辞書を辞書内に保存したいと思いますが、レベルの辞書が、私は次の操作を実行できます。

を私はNSMutableDictionaryを持って、私はNSDictionaryを持ってtopLevelDictionary と呼ばれる、myKeyと呼ばれ、私は私のキーであるNSStringを、持っているdictionaryTwo と呼ばれます。dictionaryTwoの深いコピーを作成するには

、私はそれによってdictionaryTwo変化した場合、topLevelDictionary内のオブジェクトではないでしょうtopLevelDictionarydictionaryTwoのコピーが含まれています。このように

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

を行うことができます。

+0

パーフェクト、なぜ彼らは本のようにそれを説明することはできません、笑。これを書いた後、私は辞書付きで辞書を使っていたが、辞書で辞書を使っていなかったということを理解しましたが、いくつかの誤解を覚えました。 – DIGGIDY

関連する問題