2013-03-21 4 views
0

이 가져 오기 뷰 컨트롤러가 있습니다캐치되지 않은 예외 'NSRangeException'로 인해 앱을 종료합니다. [__NSArrayM objectAtIndex :] : 인덱스 33 초과 [0 .. 32] '

저는 서버에서 xml의 이름을 추출하고로드합니다. 소자 총 수는 33

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"IMPORT"; 
    NSLog(@"User id = %@",currentUserId); 

    //some code to send http request............. 
    NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
    NSLog(@"str response:%@",str); 
    NSURL *fileURL= [[NSURL alloc] initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfURL:fileURL]; 
    GDataXMLDocument *doc =[[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil]; 

    NSArray * names = [doc nodesForXPath:@"//contacts/contact/name/firstname" error:nil]; 

    for (GDataXMLElement *element in names) { 
     // NSLog(@"name: %@ ",element.stringValue); 
     [Option addObject:element.stringValue]; 
    } 

    [Option addObject:@"nil"]; 
    NSLog(@"count: %u ",[Option count]); 

    for (CFIndex i=0; i<=[Option count]; i++) { 
     NSLog(@"options item %lu: %@\n",i,Option[i]); 
    } 
} 

오류 '* - [__ NSArrayM objectAtIndex :]의 경계를 넘어 인덱스 33 [0 ... 32] 때문에 캐치되지 않는 예외'NSRangeException '이유 응용 프로그램 종료 * 먼저 던져 호출 스택 :

답변

4

귀하의 라인

for (CFIndex i=0; i<=[Option count]; i++) 

읽어야

for (CFIndex i=0; i<[Option count]; i++) 
+0

+1 탁월한 속도 –

+0

Ahsan : 'count'는 총 개수를 제공하고 배열의 마지막 요소 색인은 그보다 작습니다. –

+0

오 감사합니다. 그것은 효과가 있었다. 아주 기본적인 실수 중 하나. –