gpt4 book ai didi

objective-c - +entityForName : nil is not a legal NSManagedObjectContext parameter searching for entity name

转载 作者:太空狗 更新时间:2023-10-30 04:02:00 45 4
gpt4 key购买 nike

我正在尝试做的事情:让 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这是有道理的。但是,他提到的方法已经写在我的应用程序委托(delegate)中,我将其导入到我正在使用的 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;
}

提前致谢!

最佳答案

错误告诉您 self.managedObjectContext 为 nil,这意味着您尚未为该属性分配值。很可能您应该在创建或加载此 View Controller 的任何代码中完成此操作(例如,如果您使用的是 Storyboard,则在 prepareForSegue:sender: 中)。

关于objective-c - +entityForName : nil is not a legal NSManagedObjectContext parameter searching for entity name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25138679/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com