【问题标题】:Terminating app due to uncaught exception 'NSRangeException'. [__NSArrayM objectAtIndex:]: index 33 beyond bounds [0 .. 32]'由于未捕获的异常“NSRangeException”而终止应用程序。 [__NSArrayM objectAtIndex:]:索引 33 超出范围 [0 .. 32]'
【发布时间】:2013-03-21 06:35:41
【问题描述】:

我有这个 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:]: index 33 beyond bounds [0 .. 32]” * 首先抛出调用栈:

【问题讨论】:

    标签: objective-c nsarray nsrangeexception


    【解决方案1】:

    你的线路

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

    应该阅读

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

    【讨论】:

    • Ahsan: count 给出总计数,而数组的最后一个元素的索引小于那个。
    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多