2016-11-03 4 views
2

내 코드에서 API를 구문 분석 할 때 __NSSingleObjectArrayI에 nil이있는 것과 같은 오류가 발생했지만 배열에 값이 있습니다. iOS에서 JSON 구문 분석시 NSSingleObjectArrayI 오류가 발생합니까?

는 여기에 코드의 내 부 ..

latitudeArray = [monumentArray valueForKey:@"lat"]; 
     NSLog(@"latitudeArray is %@",latitudeArray); 
     longitudeArray = [monumentArray valueForKey:@"lng"]; 
     NSLog(@"longitudeArray is %@",longitudeArray); 

for (int i = 0; i<3; i++) { 

      CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[latStr doubleValue] longitude:[longStr doubleValue]]; 
      CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[[latitudeArray objectAtIndex:i] doubleValue] longitude:[[longitudeArray objectAtIndex:i] doubleValue]]; ----------> This Line I Got The Crash Error.. 

      CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB]; 
      NSLog(@"distanceInMeters is %f",distanceInMeters); 
      float i = distanceInMeters/1000; 
      NSLog(@"distance between two places is %f KM", i); 

      //[locationDistances addObject:distanceInMeters]; 
     } 

NSSingleObjectArrayI의 의미는 무엇인가를 준?

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

해결 방법을 알려주십시오. latitudeArray이 값이

...

latitudeArray is (
    (
      (
        (
          (
       "27.000", 
       "27.1798189999", 
       "27.1928780000" 
      ) 
     ) 
    ) 
) 

)

+0

이 배열을 tableview에서 사용하셨습니까? – KKRocks

+0

아니요, API에서 응답을 받았고 배열에 저장되어 있습니다. @ KKRocks –

+0

어떤 코드를 보여 주시겠습니까? 충돌 결과는 무엇입니까? – dirtydanee

답변

3

[latitudeArray objectAtIndex:i] 또는 [longitudeArray objectAtIndex:i]는 특정 인덱스에 액세스하려고 할 때 충분하지 않은 항목이 있습니다.

충돌을 피하기 위해 할 일은 특정 인덱스의 개체에 액세스하기 전에 배열이 i 값보다 크지 않은지 여부를 확인하는 것입니다.

for (int i = 0; i<3; i++) { 
     if ([latitudeArray count] > i && [longitudeArray count] > i) { 
      CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[latStr doubleValue] longitude:[longStr doubleValue]]; 
      CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[[latitudeArray objectAtIndex:i] doubleValue] longitude:[[longitudeArray objectAtIndex:i] doubleValue]]; 

      CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB]; 
      NSLog(@"distanceInMeters is %f",distanceInMeters); 
      float i = distanceInMeters/1000; 
      NSLog(@"distance between two places is %f KM", i); 
     } 
    } 
+0

답장을 보내 주셔서 감사합니다. @ dirtydanee –

1

이 시도 :

if (latitudeArray.count == longitudeArray.count) { 
     for (int i = 0; i<latitudeArray.count; i++) { 

     } 
    } 
6

먼저 약간의 혼동을 취소 : NSSingleObjectArray은 단일 요소 배열에있는 NSArray 단지 특별한 경우이다; 꽤 많은 일이 발생하고 특별한 경우 작은 물건을 가지고 있으면 그 공간을 절약 할 수 있습니다. (싱글 톤 인스턴스를 가진 빈 배열을위한 클래스조차 존재하며, 모든 빈 배열은 같은 객체입니다!). 그래서 그것이 NSArray 척.

예외는 분명히 존재하지 않는 배열 요소에 액세스하려했습니다. 배열에 요소가 하나만있을 때 인덱스 1의 요소에 액세스하려고 했으므로 인덱스 0의 요소에만 액세스 할 수있었습니다.

longitudeArray가 세 가지 요소를 가질 것으로 예상되지만 분명히 하나만 있습니다. 그리고 monumentArray는 배열이 아니므로 배열로 호출하지 마십시오. 서버가 예상 정확히 무엇을 보내지 않는 경우

당신이 JSON을 처리

, 당신은 받은 데이터를, 당신이 기대했던 것을 체크 그렇지 않으면 응용 프로그램 여기 저기 충돌 끝날해야합니다.

관련 문제