在ViewDidLoad上获取正在崩溃的应用程序

时间:2011-07-30 10:31:29

标签: iphone objective-c xcode cocoa-touch nsfetchedresultscontroller

在我的viewDidLoad中,我self.todaySession = (id)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];在找到此对象时工作正常。但是当它没有时,它会崩溃应用程序。有没有一个逻辑状态可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

根据具体情况,有几种方法可以做到这一点。

您可以使用fetchedObjects中的sectionsNSFetchedResultsController计数。请务必事先致电performFetch:

    [fetchedResultsController performFetch:self];

    // Option 1
    BOOL someResultsReturned = ([[fetchedResultsController fetchedObjects] count] > 0);
    // Option 2
    BOOL someResultsReturned = ([[fetchedResultsController sections] count] > 0);

    if (someResultsReturned) {
        self.todaySession = (id)[fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    } else {
        // Handle no results here
    }

更多信息

Read more about NSFetchedResultsController here