2016-12-03 34 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; 
    ... 
} 

我怎样才能做到这一点?

+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) 

注意日期和关闭数组必须具有相同的长度

0

假设“日期”和closes元素的数量是相等的:

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

取决于你是如何确定和如何你需要这些代码来证明你的愚蠢,你可能想要添加检查(和错误处理)来覆盖上述假设。

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]]; 
} 
相关问题