我的應用程序有多個視圖控制器。在我的VehicleListController中,我將數據保存到核心數據。在FavouritesController中,我從核心數據中獲取數據以在表格視圖中顯示它。核心數據錯誤iPhone
我收到此錯誤,同時檢查favouritesController表來查看核心數據。
'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Favouritesdata''
實體名稱是Favouritesdata。
在應用程序委託方法didFinishLaunchingWithOptions是
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *user = [defaults objectForKey:@"Username"];
NSString *passwd = [defaults objectForKey:@"Password"];
if ((user != nil) && (passwd != nil)) {
NSLog(@"Data found");
self.progressView = [[Progressbar alloc] initWithNibName:@"Progressbar" bundle:nil];
self.window.rootViewController = self.progressView;
[self.window makeKeyAndVisible];
}
else {
NSLog(@"No data saved");
self.viewController = [[VektorViewController alloc] initWithNibName:@"VektorViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
return YES;}
在favouritesController.m獲取數據
-(void)getData {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favouritesdata" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setFetchBatchSize:20];
[request setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"licensePlate" ascending:YES];
NSArray *newArray = [NSArray arrayWithObject:sort];
[request setSortDescriptors:newArray];
NSError *error;
NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
[self setLicensePlateArray:results];
[self.favouritesTable reloadData];
}
而在的cellForRowAtIndexPath在favouritesController
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILongPressGestureRecognizer *pressRecongnizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tableCellPressed:)];
pressRecongnizer.minimumPressDuration = 0.5f;
[cell addGestureRecognizer:pressRecongnizer];
[pressRecongnizer release];
}
Favouritesdata *favdata = [licensePlateArray objectAtIndex:indexPath.row];
NSLog(@"favdata: %@", favData);
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
cell.textLabel.text =
[self.filteredListItems objectAtIndex:indexPath.row];
}
else{
cell.textLabel.text = [favdata licenseplate];
// [self.licensePlateArray objectAtIndex:indexPath.row];
}
return cell;}
在應用程序核心數據存取delegate.m:
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
stringByAppendingPathComponent: @"LoginTest.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeUrl options:nil error:&error]) {
/*Error for store creation should be handled in here*/
}
return persistentStoreCoordinator;}
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];}
我已將核心數據添加到現有項目。現在我不知道爲什麼我得到這個錯誤,而我認爲我已經將所有必需的方法和框架複製到了我的項目中。
有誰能告訴我嗎?
我編輯了我的問題,並添加了核心訪問者數據,請檢查它 –