我已经通过 Core Data 创建了一个简单的 1 实体数据模型。我在“AppDelegate”的“didFinishLaunchingWithOptions”中添加了一个实体。然后,我试图将模型的数据加载到一个数组,然后是一个 TableView 。 我知道有类似的帖子,但我仍然无法找出我的代码有什么问题。
- 我尝试过和检查过的事情列表:
- 实体已正确添加到模型中
- 我已经检查过实体名称,它们是正确的。
- 我已经尝试删除该应用程序并彻底重建它,但仍然没有任何效果。
所以我的代码肯定有更严重的问题。感谢您的帮助。
这是我的错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'FavoritesInfo'
AppDelegate.h
#import <UIKit/UIKit.h>
#import "favTable.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) favTable *viewController;
@property (strong, nonatomic) UINavigationController *navController;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory; // reference files for core data
@end
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize navController = _navController;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *favoritesInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"FavoritesInfo"
inManagedObjectContext:context];
[favoritesInfo setValue:@"Product 1" forKey:@"name"];
[favoritesInfo setValue:[NSNumber numberWithInt:15] forKey:@"score"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
return YES;
}
然后在tableviewcontroller的类中favtable.h
#import <UIKit/UIKit.h>
@interface favTable : UITableViewController <NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
NSArray *favArr;
int num;
}
@property (nonatomic, strong) NSArray *favArr;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@end
favtable.m viewdidload
#import "favTable.h"
#import "AppDelegate.h"
@interface favTable ()
@end
@implementation favTable
@synthesize favArr;
@synthesize spVC;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize fetchedResultsController;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Favorites";
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error=nil;
self.favArr=[managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error!=nil) {
NSLog(@" fetchError=%@,details=%@",error,error.userInfo);
}
}
最佳答案
您的 managedObjectContext 未在您的 ViewController 中设置。 在您的 AppDelegate didFinishLaunchingWithOptions 中设置它,通常如下所示:
ViewControllerClass *controller = (ViewControllerClass*)self.window.rootViewController;
或使用导航 Controller :
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
ViewControllerClass *controller = (ViewControllerClass *)navigationController.topViewController;
然后:
controller.managedObjectContext = self.managedObjectContext;
关于ios - 核心数据 - 无法加载数据,找不到实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24752400/