2016-12-03 6 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

당신은이 배열이 있고 당신은 당신이 이미 그것을 설정 값으로 대체 할 사전이있는 경우 다음 _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]]; 
}