运行应用时出现此错误:
*** 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,应用程序在应用程序启动时加载此文件。
TimeTableController.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)添加到现有项目中。
我尝试了所有找到的解决方案,但我不知道为什么我现在收到此错误。
注意:我知道有很多线程都涵盖了这些问题。我看了很多,但没有一个答案对我不起作用。
答案 0 :(得分:-1)
我收到了相同的错误消息,结果发现managedObjectContext为nil。