0

我有这个 ImportViewController.m

我正在从服务器的 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]);
    }
}

错误:由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:' * -[__NSArrayM objectAtIndex:]:索引 33 超出范围 [0 .. 32]' *第一次抛出调用堆栈:

4

1 回答 1

4

你的线

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

应该读

for (CFIndex i=0; i<[Option count]; i++)
于 2013-03-21T06:38:14.060 回答