为什么这会产生错误 - 鬼魂NSArray?

时间:2011-12-28 01:08:47

标签: ios uitableview nsarray

当我启用(取消注释)这行代码时,我得到一个SIGABRT崩溃 - 这对我来说很奇怪,因为我之前从未遇到过这个问题。

cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title];

我得到的错误是:

  

2011-12-28 14:03:00.118 MapWithTableModalView-001 [2006:11603]本地化名称数据库不存在   2011-12-28 14:03:01.110 MapWithTableModalView-001 [2006:11603] *由于未捕获的异常'NSRangeException'终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引0超出空数组'

的边界

项目不会产生任何错误或警告。

该项目基于2个UIViewControllers,它们通过一个按钮 - ModalViewContoller相互滑动。当我按下按钮时,发生了崩溃。

只是在这里添加代码 - 它是教程的一部分,但我仍然希望得到它:-)我知道这看起来不像普通数组,但数据在项目中设置如下。一旦我搞清楚了,我想把它设置在Plist中。

-(void)loadOurAnnotations
    {
CLLocationCoordinate2D workingCoordinate;


    #pragma mark - Apple Stores
workingCoordinate.latitude = 51.514298;
workingCoordinate.longitude = -73.973034;
iCodeBlogAnnotation *appleStore1 = [[iCodeBlogAnnotation alloc]     initWithCoordinate:workingCoordinate];
    [appleStore1 setTitle:@"Apple Store 5th Ave."];
[appleStore1 setSubtitle:@"Apple's Flagship Store"];
[appleStore1 setAnnotationType:iCodeBlogAnnotationTypeApple];

[viewController.mapView addAnnotation:appleStore1];

workingCoordinate.latitude = 51.514298;
workingCoordinate.longitude = -0.141949;
iCodeBlogAnnotation *appleStore2 = [[iCodeBlogAnnotation alloc] initWithCoordinate:workingCoordinate];
[appleStore2 setTitle:@"Apple Store St. Regent"];
[appleStore2 setSubtitle:@"London England"];
[appleStore2 setAnnotationType:iCodeBlogAnnotationTypeApple];

[viewController.mapView addAnnotation:appleStore2];



     }

如果您希望我发布任何其他代码,我会很高兴,但我不知道在哪里查看,因此不知道要发布哪个位有用。

-Jeff

1 个答案:

答案 0 :(得分:2)

您的注释数组为空(零元素)。

查看您的-loadOurAnnotations方法,您实际上似乎从未设置过您正在阅读的annotations数组。尝试将该行更新为以下内容,看看是否有帮助:

// I'm not sure where viewController comes from but I'll assume it's a method on this
// class and that it's the same class that defines -loadOurAnnotations. If it's not,
// update this appropriately to get access to the mapView to which you've added
// the annotations
cell.textLabel.text = [[[[viewController mapView] annotations] objectAtIndex:indexPath.row] title];