运行应用程序时出现此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity
name 'Event''
这是我的 TimeTableController.h 文件(TimeTableController 是 UITableViewController 的子类,当应用程序启动时应用程序会加载此文件。
时间表控制器.h:
#import "Event.h"
@interface TimeTableController : UITableViewController
{
NSManagedObjectContext *managedObjectContext;
NSMutableArray *eventArray;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *eventArray;
- (void) fetchRecords;
- (void) addTime:(id)sender;
@end
Event.h 和 Event.m 是我的模型文件。
和 TimeTableController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Lap Times";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addTime:)];
self.navigationItem.rightBarButtonItem = addButton;
//[addButton release];
[self fetchRecords];
// 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;
}
-(void)fetchRecords {
// This line gives the error
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
}
fetchRecords 方法中的行给出了错误。
这是我的 AppDelegate.m 文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
TimeTableController *tableController = [[TimeTableController alloc] initWithStyle:UITableViewStylePlain];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
//[tableController release];
[window addSubview: [self.navigationController view]];
[window makeKeyAndVisible];
return YES;
}
创建项目时,数据模型文件不存在,因此我将数据模型文件 (LapTimer.xcdatamodeld) 添加到现有项目中。
我尝试了所有找到的解决方案,但我不知道为什么现在会出现此错误。
注意:我知道有很多线程完全涵盖了这个问题。我看了所有,但没有一个答案对我不起作用。