0

I have this ImportViewController.m

i am extracting and loading names from xml from server. total number of elements is 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]);
    }
}

Error: Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 33 beyond bounds [0 .. 32]' * First throw call stack:

meth
  • 1,887
  • 2
  • 18
  • 33

1 Answers1

4

Your line

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

should read

for (CFIndex i=0; i<[Option count]; i++)
Richard Brown
  • 11,346
  • 4
  • 32
  • 43