在我的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设置为此控制器? 最佳答案
因为您正在记录对象@“ numberOfRowsInSection%@”,而numberOfObjects是一个int,所以:
@property (nonatomic, readonly) NSUInteger numberOfObjects
使用@“ numberOfRowsInSection%i”代替。