【问题标题】:+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name+entityForName: nil 不是搜索实体名称的合法 NSManagedObjectContext 参数
【发布时间】: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 问题都没有(包括 thisthis) 有一个解释,除了 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


    【解决方案1】:

    错误告诉您self.managedObjectContext 为 nil,这意味着您尚未为该属性分配值。您很可能应该在创建或加载此视图控制器的任何代码中执行此操作(例如,如果您使用故事板,则在 prepareForSegue:sender: 中)。

    【讨论】:

      猜你喜欢
      • 2015-07-06
      • 2013-12-26
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多