2016-09-24 56 views
0

我知道這可能是一個重複的問題,但我GOOGLE了很多,但無法爲我找到合適的答案。NSDictionary獲取重複值

我有一個NSMutableArray它有twoNSDictionary與密鑰和值,我需要在UITableView填充。我已經檢索到該我去填充使用

NSMutableArray *mutArray = [responseArray valueForKey:@"Table"]; 

的的dictionary價值和我不喜歡

NSMutableSet *names = [NSMutableSet set]; 
NSMutableArray *mutArray1 = [[NSMutableArray alloc] init]; 

for (id obj in mutArray) { 

    NSString *destinationName = [obj valueForKey:@"AssetClassName"]; 

    if (![names containsObject:destinationName]) { 

     [mutArray1 addObject:destinationName]; 
     [names addObject:destinationName]; 

    } 
} 

因爲值AssetClassName重複。現在我有mutArray1三個值,我需要顯示爲UITableView部分。在AssetClassName下我有一些數據確定該部分的行。

用於檢索數據,我做這樣

for (int i = 0; i < [mutArray1 count]; i++) { 

    NSMutableDictionary *a = [[NSMutableDictionary alloc] init]; 
    NSMutableDictionary *b = [[NSMutableDictionary alloc] init]; 

    for (NSDictionary *dict in mutArray) { 

     if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) { 

      [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"]; 
      [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"]; 
      [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"]; 
      [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"]; 

      [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]]; 

      [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]]; 
     } 
    } 
} 

mutdictNSMutableDictionary全局聲明並實例化viewdidLoad

mutdict = [[NSMutableDictionary alloc] init]; 

因爲我需要的值插入mutdict。相應地,每個SubAssetClassName被添加到AssetclassName中。

但我的問題是在我的最終字典,即mutdictSubAssetClassName的值重複。

有人可以告訴如何解決這個問題。

我的控制檯

"AssetClassName" =  { 

    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "HIGH YIELD BONDS"; 
     "ModelAllocationPercentage" = 22; 
    }; 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "HIGH YIELD BONDS"; 
     "ModelAllocationPercentage" = 22; 
    }; 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "HIGH YIELD BONDS"; 
     "ModelAllocationPercentage" = 22; 
    }; 
}; 
"AssetClassName" =  { 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "EMERGING MARKETS EQUITIES"; 
     "ModelAllocationPercentage" = 10; 
    }; 
}; 
"AssetClassName" =  { 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "STRUCTURED PRODUCTS"; 
     "ModelAllocationPercentage" = 10; 
    }; 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "STRUCTURED PRODUCTS"; 
     "ModelAllocationPercentage" = 10; 
    }; 
    "SubAssetClass" =   { 
     "%" = 0; 
     "Amount (EUR)" = 0; 
     "Investment Categories" = "STRUCTURED PRODUCTS"; 
     "ModelAllocationPercentage" = 10; 
    }; 
}; 
} 

在這裏我可以看到,所有SubAssetClass值相同的每個部分,但實際上它不是。

我該如何解決這個問題。

回答

1

您需要在循環內創建一個新的mutable字典實例。現在你創建一個實例並反覆更新它。這導致一個字典被反覆添加。

更改您的代碼如下:

for (NSInteger i = 0; i < [mutArray1 count]; i++) { 
    NSMutableDictionary *b = [[NSMutableDictionary alloc] init]; 

    for (NSDictionary *dict in mutArray) { 
     if ([[mutArray1 objectAtIndex:i] isEqualToString:[dict valueForKey:@"AssetClassName"]]) { 
      NSMutableDictionary *a = [[NSMutableDictionary alloc] init]; 

      [a setObject:[dict objectForKey: @"SubAssetClassName"] forKey:@"Investment Categories"]; 
      [a setObject:[dict valueForKey:@"Amount"] forKey:@"Amount (EUR)"]; 
      [a setObject:[dict valueForKey:@"AllocationPercentage"] forKey:@"%"]; 
      [a setObject:[dict valueForKey:@"ModelAllocationPercentage"] forKey:@"ModelAllocationPercentage"]; 

      [b setObject:a forKey:[dict valueForKey:@"SubAssetClassName"]]; 

      [mutdict setObject:b forKey:[dict valueForKey:@"AssetClassName"]]; 
     } 
    } 
} 

而且,在大多數情況下,你不應該使用valueForKey:。使用objectForKey:,除非您明確且特別需要使用鍵值編碼,而不是僅僅從字典中爲給定鍵獲取對象。

+0

謝謝你....在我的UITableView單元格中的一個更多的問題我得到這些值像'NSDictionary * rowData = [mutdict valueForKeyPath:[sectionArray objectAtIndex:indexPath.section]];'但不知道如何用SubAssetClass名稱和值填充每個單元格。你能幫忙嗎? –

+0

如果您還有其他問題,請發佈新問題。一定要在問題中包含所有相關細節。 – rmaddy

+0

好吧,你說我問另一個問題,你可以檢查在[鏈接](http://stackoverflow.com/questions/39680845/populate-nsmutabledictionary-value-to-uitableviewcell) –