无法找到实体名称“X”的NSManagedObjectModel

时间:2012-02-09 22:55:52

标签: core-data ios5 nsmanagedobjectcontext

我遇到此崩溃错误: * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'+ entityForName:找不到实体名称的'NSManagedObjectModel''JourneeDeTravail''

我的AppDelegate.h:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;     
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator          *persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
@end

我的AppDelegate.m:

#import "AppDelegate.h"
#import "TableViewController.h"
#import "ViewController.h"

@implementation AppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize managedObjectContext =_managedObjectContext;
@synthesize managedObjectModel =_managedObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *viewController = [[ViewController alloc] init];
viewController.managedObjectContext = [self managedObjectContext];
NSLog(@"AppDelegate VC: %@",  managedObjectContext);
return YES;
}

- (void)dealloc {
[super dealloc];
}

@end

ViewController.h:

#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController{
NSManagedObjectContext *managedObjectContext;
}
@property (retain, nonatomic) IBOutlet UILabel *displayStart;
@property (retain, nonatomic) IBOutlet UILabel *displayEnd;
@property (retain, nonatomic) IBOutlet UITextField *displayResult;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

- (IBAction)SaveTest:(id)sender;

@end

和ViewController.m:

#import "ViewController.h"

@implementation ViewController
@synthesize managedObjectContext;

- (void)viewDidLoad {    
if (managedObjectContext == nil) { 
    managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate]managedObjectContext];
    NSLog(@"After managedObjectContext VC: %@",  managedObjectContext);
}
}

- (IBAction)SaveTest:(id)sender {
NSLog(@"Dans SaveTest : %@",  managedObjectContext);
NSLog(@"Context: %@",managedObjectContext);
NSLog(@"PS Coord : %@",managedObjectContext.persistentStoreCoordinator);
NSLog(@"MOM : %@", managedObjectContext.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",  [[managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"JourneeDeTravail"]);
JourneeDeTravail *journee = (JourneeDeTravail *)[NSEntityDescription insertNewObjectForEntityForName:@"JourneeDeTravail" inManagedObjectContext:managedObjectContext];
}

但是当我按下SaveTest按钮时,它崩溃并显示错误日志,说我的所有日​​志行都是(null)。

当然我有一个名为JourneeDeTravail的实体......

有什么想法吗?看起来我没有managedObjectContext,但我不知道如何解决这个问题。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

对于那些遇到同样问题的人:我在完成本教程时找到了解决方案:http://www.techotopia.com/index.php/An_iOS_5_iPhone_Core_Data_Tutorial

答案 1 :(得分:1)

调试器说全部。它无法找到实体。在您的情况下,实体是存储。没有核心数据,没有内存。

在AppDlegate.m中,在applicationDidFinishLaunching尝试实例化存储类时。这是ManageObjectModel搜索的权利。

喜欢:myMainThreadStorage = [[StorageClass alloc] init];

或者我完全误解了过去6个小时研究我的问题并用后来的[super viewDidLoad]

解决了