2016-12-03 12 views
1

サーバーからデータを取得する要求を投稿しました。今、NSDictionaryに特定のデータがあります。チャートに表示する必要があるので、データを適切なものに変換する必要があります。NSMutableDictionaryの値を別の値に設定する

dates =  (
     "01.01.2016", 
     "01.01.2016", 
     "01.01.2016" 
     ...) 

closes =  { 
     0 =   (
      1, 
      1, 
      1, 
      ...) 
     } 

が、私はこの形式に変換する必要があります:ここで私は、サーバーから取得したデータです

{ 
    "01.01.2016" = 1; 
    "01.01.2016" = 1; 
    "01.01.2016" = 1; 
    ... 
} 

私はこれをどのように行うことができますか?日付と配列は、「日付」とcloses要素の数を仮定すると、同じ長さ

+0

を置き換えるために辞書をたいている場合ですあなたの本名はスティーブジョブズですか? –

答えて

0
NSArray *dates = ... 
NSArray *closes = ... 

NSMutableDictionary *result = [NSMutableDictionary new]; 
for (int i = 0; i < dates.count; i++) { 
    result[dates[i]] = closes[i]; 
} 
NSLog(@"%@", result) 

注等しい:

NSMutableDictionary dataOut = [NSMutableDictionary dictionary]; 
for (int index = 0; index < dataIn[@"dates"].count; index++) 
{ 
    dataOut[dataIn[@"dates"][index]] = dataIn[@"closes"][index]; 
} 

あなたはどのように特定の上で、どのように応じて、あなたがこのコードを必要とすることを偽証するならば、上記の前提をカバーするチェック(およびエラー処理)を追加したいかもしれません。

0

を持っている必要があります閉じ

0

あなたが2列に持っていて、辞書を作成する必要がある場合は、 _productArray2 & _productArray1

NSDictionary * productDictionary = [NSMutableDictionary dictionary]; 
    for (NSUInteger index =0; index < _productArray1.count; index ++) { 
     [productDictionary setValue:_productArray1[index] forKey:_productArray2[index]]; 
    } 

、あなたはすでにそれに設定値の

NSArray * allKeys = [productDictionary allKeys]; 

for (NSUInteger index =0; index < allKeys.count; index ++) { 
    [productDictionary setValue:_productArray[index] forKey:allKeys [index]]; 
} 
関連する問題