0

我有Maintableview(a UIViewController) 的内容和detailview(a UIViewController) 显示 Maintableview 中每个单元格的详细信息。
我在“ detailview ”中添加了一个“收藏夹”按钮,用户可以添加到Favoritetableview(a UIViewController) 中,并且一切正常。

现在我Maintableview通过向左滑动添加了“添加到收藏夹”。它成功地添加了内容,Favoritetableview但是当我在应用程序中触摸该单元格时Favoritetableview会崩溃。

以下是崩溃的控制台日志:

 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a871d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a2e3deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a75a934 -[__NSArrayI objectAtIndex:] + 164
    3   iranbirds                           0x00000001075cf672 -[FavoriteTableViewController prepareForSegue:sender:] + 530
    4   UIKit                               0x00000001090c55d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
    5   UIKit                               0x00000001090c5433 -[UIStoryboardSegueTemplate _perform:] + 82
    6   UIKit                               0x0000000108b1b5f8 -[UIViewController performSegueWithIdentifier:sender:] + 99
    7   iranbirds                           0x00000001075cf42d -[FavoriteTableViewController tableView:didSelectRowAtIndexPath:] + 189
    8   UIKit                               0x0000000108ac51c6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1887
    9   UIKit                               0x0000000108ac541b -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
    10  UIKit                               0x0000000108989f62 _runAfterCACommitDeferredBlocks + 317
    11  UIKit                               0x000000010899de4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
    12  UIKit                               0x00000001089aa147 _afterCACommitHandler + 90
    13  CoreFoundation                      0x000000010a796c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    14  CoreFoundation                      0x000000010a796ba7 __CFRunLoopDoObservers + 391
    15  CoreFoundation                      0x000000010a78c7fb __CFRunLoopRun + 1147
    16  CoreFoundation                      0x000000010a78c0f8 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x000000010be6bad2 GSEventRunModal + 161
    18  UIKit                               0x000000010897df09 UIApplicationMain + 171
    19  iranbirds                           0x00000001075d32ef main + 111
    20  libdyld.dylib                       0x000000010ba6492d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最喜欢的表格视图:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = (NSIndexPath *)sender;
    Favorite *fav = (Favorite *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *combinedName = fav.name;

    if  ([segue.identifier isEqualToString:@"FavoriteBirdDetail"])
    {
        GeneralViewController *detailViewController = (GeneralViewController*)[segue destinationViewController];

        detailViewController.birdName = [combinedName componentsSeparatedByString:@"^"][0];;
        detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];;
        detailViewController.managedOjbectContext = self.managedOjbectContext;
    }

}

此行导致错误:
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

任何帮助,将不胜感激。

4

1 回答 1

0
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

好吧,看看这段代码——你正在通过下标语法从数组中获取第二个对象。如果数组包含至少 2 个项目,则该代码将起作用。

看起来您的数组包含零个或一个元素,这就是为什么当您尝试通过 1 个索引 - 获取元素时应用程序崩溃的原因[1],这意味着第二个元素。

为防止崩溃,您应该在之前检查数组count属性:

NSArray *anArray = [combinedName componentsSeparatedByString:@"^"]; 

if anArray.count >= 2 {
    detailViewController.sciName = anArray[1];
}
于 2016-08-09T15:25:55.357 回答