2014-04-21 6 views
0

그래프 작성을위한 일련의 점을 채우려고합니다. I를 사용하여 개별 값에 액세스 할 수Json 응답에서 NSMutableArray를 채우십시오.

(
    { 
    precipIntensity = 0; 
    precipProbability = 0; 
    time = 1398123660; 
}, 
    { 
    precipIntensity = 0; 
    precipProbability = 0; 
    time = 1398123720; 
}, 
    { 
    precipIntensity = 0; 
    precipProbability = 0; 
    time = 1398123780; 
}) 

: i는 인덱스 같다

responseArray[i][@"precipProbability"] 

여기서 반환되는 데이터의 일례이다.

이러한 점으로 배열을 채우기 위해 노력하고 있지만 작동하지 않을 수 있습니다. 누군가 도울 수 있습니까?

이 나는 ​​시도했다 :

if (!responseArray || !responseArray.count){ 
} else { 
    for (int i=0; i > [responseArray count]; i++) { 
     ArrayOfValues[i] = responseArray[i][@"precipProbability"]; 
    } 

if (!responseArray || !responseArray.count){ 
} else { 
    for (int i=0; i > [responseArray count]; i++) { 
     [ArrayOfValues insertObject:responseArray[i][@"precipProbability"] atIndex:i]; 
    } 

과 ArrayOfValues는 항상 빈 (NULL)입니다.

+1

후 당신은 문제가있는 코드. 문제를 지적하십시오. – rmaddy

+0

ArrayOfValues를 초기화 했습니까? 또한 변수 이름에 camelCasing을 사용하십시오. 클래스와 구별하려면'arrayOfValues'이어야합니다. –

+0

i> [responseArray count]가 분명히 잘못되었습니다. 그리고 나는 "if (responseArray == nil || responseArray.count == 0)"또는 "if (responseArray.count == 0)"를 작성하는 것이 좋습니다. 아니면 더 나은 그냥 "for (NSDictionary * dict in responseArray)"; responseArray == nil 또는 responseArray.count에 대한 검사가 필요하지 않습니다. – gnasher729

답변

1

for 루프 상태가 거꾸로입니다. 원하는 항목 :

if (!responseArray || !responseArray.count){ 
} else { 
    for (NSUInteger i = 0; i < [responseArray count]; i++) { 
     [ArrayOfValues addObject:responseArray[i][@"precipProbability"]]; 
    } 

올바른 데이터 형식은 i입니다. 이것에 대해

0

데이터가 이미 배열에있는 것처럼 보입니다. 당신이 변경 가능한 배열에 복사하려는 경우 당신의 진짜 목표는의 Obj-C 모델 객체로 JSON 개체를 구문 분석하는 경우

NSMutableArray *mut = [responseArray mutableCopy]; 

, 당신은 아마 <NSFastEnumeration>

에 위로 읽고 싶은

당신이 호출 할 수 있습니다
0

방법 :

NSArray *array = [responseArray valueForKey:@"precipProbability"];