2011-01-13 96 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