gpt4 book ai didi

iphone - 核心数据 iPhone : could not locate an NSManagedObjectModel

转载 作者:IT老高 更新时间:2023-10-28 11:50:09 25 4
gpt4 key购买 nike

我正在使用 Apple 的 CoreDataBooks 示例项目作为核心数据的学习辅助工具。

我修改了应用程序,以便在加载应用程序时首先显示菜单页面 - 而不是 Books TableView (RootViewController)。

我做了以下事情:

我在界面生成器中创建了一个菜单页面(只是一个带有按钮的 View )

CoreDataBooksAppDelegate.h 现在看起来像这样:

// for the menu
@class MenuViewController;

@interface CoreDataBooksAppDelegate : NSObject <UIApplicationDelegate> {

NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;

UIWindow *window;
UINavigationController *navigationController;

//for the menu
MenuViewController *viewController;
}

- (IBAction)saveAction:sender;

//for the menu
@property (nonatomic, retain) IBOutlet MenuViewController *viewController;

@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

@property (nonatomic, readonly) NSString *applicationDocumentsDirectory;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

CoreDataBooksAppDelegate.m 如下所示:

#import "CoreDataBooksAppDelegate.h"
#import "RootViewController.h"
// for the menu
#import "MenuViewController.h"


@implementation CoreDataBooksAppDelegate

@synthesize window;
@synthesize navigationController;

// for the menu
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (void)applicationDidFinishLaunching:(UIApplication *)application {

RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;

// for the menu
[window addSubview:viewController.view];

// Configure and show the window
[window makeKeyAndVisible];
}

CoreDataAppDelegete.m 的其余部分保持不变。

在MenuViewController中,当按钮被点击时,会发生以下 Action :

RootViewController *modalViewController1 = [[[RootViewController alloc] initWithNibName:nil bundle:nil] autorelease];  
[self presentModalViewController:modalViewController1 animated:YES];

在 IB 中,我将 MainWindow.xib 更改为调用 MenuViewController 而不是 RootViewController。

因此,应用程序已加载,并且菜单通过按钮正确显示。单击按钮后,应用程序在 RootViewController 的 viewDidLoad 内崩溃。

这里崩溃了:

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"1 START viewDidLoad RootViewController");
self.title = @"Books";
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

NSLog(@"2 setup button viewDidLoad RootViewController");
// Configure the add button.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBook)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];

NSLog(@"3 viewDidLoad RootViewController");
NSError *error;
// HERE IS THE CRASH SITE
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Does not reach this point in viewDidLoad RootViewController");
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort(); // Fail
}
NSLog(@"END viewDidLoad RootViewController");
}

在控制台中我收到以下信息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Book''

我已阅读有关此异常的信息,但我不知道解决它的正确步骤。

最佳答案

好的。

将以下代码放在 RootViewController 的 viewDidLoad 中可消除错误:

if (managedObjectContext == nil) 
{
managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}

我在 SO:link text 上发现有人遇到类似问题

正如 Aryeh 在该帖子中指出的那样:“简而言之,您正在尝试从尚未设置的 objectContext 中获取实体。因此,您的选择是立即设置它,或者之前在应用程序的其他地方进行设置加载此 View 。”

关于iphone - 核心数据 iPhone : could not locate an NSManagedObjectModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1632497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com