我通过Core Data创建了一个简单的1实体数据模型。我在" didFinishLaunchingWithOptions"中添加了一个实体。 " AppDelegate"。然后,我试图将模型的数据加载到数组,然后加载表视图。 我知道有类似的帖子,但我仍然无法找出我的代码有什么问题。
因此我的代码必须存在更大的问题。任何帮助表示赞赏。
这是我的错误:
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;
}
然后在表viewcontroller中使用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);
}
}
答案 0 :(得分:1)
您的ViewController中未设置您的managedObjectContext。 在AppDelegate中设置didFinishLaunchingWithOptions,通常如下所示:
ViewControllerClass *controller = (ViewControllerClass*)self.window.rootViewController;
或使用导航控制器:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
ViewControllerClass *controller = (ViewControllerClass *)navigationController.topViewController;
然后:
controller.managedObjectContext = self.managedObjectContext;
答案 1 :(得分:0)
在我的favtable.m类中,我添加了这个来设置managedObjectContext并且它可以正常工作。
self.managedObjectContext = ((AppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext;