【发布时间】:2014-09-28 02:34:31
【问题描述】:
我正在尝试做的事情:让 NSFetchedResultsController 第一次工作以将 UITableView 连接到 Core Data。我是一个新手,非常缺乏经验,我已经逐字关注Ray Wenderlich's Tutorial。
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Medicine''`.
我尝试了什么:我发现的其他 StackOverflow 问题都没有(包括 this 和 this) 有一个解释,除了 the second one 这是有道理的。但是,他提到的方法已经写在我的应用程序委托中,我将其导入到我正在使用的 TableViewController 类中。
非常感谢:解释我在做什么/出了什么问题以及如何解决它,而不仅仅是答案/更正。
PMMedicinesTableViewController.m
#import "PMMedicinesTableViewController.h"
#import "PMAppDelegate.h"
#import "Medicine.h"
@interface PMMedicinesTableViewController ()
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@end
@implementation PMMedicinesTableViewController
@synthesize fetchedResultsController = _fetchedResultsController;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Medicine" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Medicine *med = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = med.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",
med.name];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"medicineCellIdentifier" forIndexPath:indexPath];
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
提前致谢!
【问题讨论】:
标签: objective-c uitableview core-data nsfetchedresultscontroller nsmanagedobjectcontext