我有这个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]);
}
}
错误:由于未捕获的异常终止应用&#39; NSRangeException&#39;,原因:&#39; * - [__ NSArrayM objectAtIndex:]:索引33超出范围[0 .. 32]&#39 ; * 首先抛出调用堆栈:
答案 0 :(得分:4)
你的行
for (CFIndex i=0; i<=[Option count]; i++)
应该阅读
for (CFIndex i=0; i<[Option count]; i++)