【问题标题】:EXC_BAD_ACCESS: Core Data, NSFetchedResultsController and numberOfRowsInSectionEXC_BAD_ACCESS:核心数据、NSFetchedResultsController 和 numberOfRowsInSection
【发布时间】:2011-01-29 05:09:52
【问题描述】:

在我的 UITableViewController 类中,我有一个用于 NSFetchResultsController 的 getter 函数

-(NSFetchedResultsController*) fetchedResultsController {

    if (_fetchedResultsController == nil) {
        FlipPadAppDelegate* app = (FlipPadAppDelegate*) [[UIApplication sharedApplication] delegate];
        NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:
            [NSEntityDescription entityForName:@"FundClass" inManagedObjectContext:[app managedObjectContext]]];
        NSSortDescriptor* sortdesp = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortdesp]];

        [fetchRequest setFetchBatchSize:20];

        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                       managedObjectContext:[app managedObjectContext]
                                                                         sectionNameKeyPath:nil 
                                                                                  cacheName:@"_Stock"];


        _fetchedResultsController.delegate = self;  
        [fetchRequest release];
        [sortdesp release];

        NSError *error;
        if (![[self fetchedResultsController] performFetch:&error]) {
            // Handle error
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

        }
    }

    return _fetchedResultsController;

}

不知何故,当应用程序运行到这一点时,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];
    NSLog(@"numberOfRowsInSection %@", [sectionInfo numberOfObjects]);
    return [sectionInfo numberOfObjects];

}

它会触发EXC_BAD_ACCESS

sqllite 数据库中有两条记录。

为什么会有例外?是因为我将sectionNameKeyPath 设置为nil 吗?还是因为我没有给这个控制器设置视图的dataSource?

【问题讨论】:

    标签: objective-c ios core-data


    【解决方案1】:

    因为您正在记录一个对象 @"numberOfRowsInSection %@" 并且 numberOfObjects 是一个 int:

    @property (nonatomic, readonly) NSUInteger numberOfObjects
    

    请改用@"numberOfRowsInSection %i"。

    【讨论】:

    • 非常感谢!这正是问题所在!几天来我一直在研究文档,但我从没想过这可能是 NSLog/格式字符串问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多