我如何解决NSInternalInconsistencyException',原因:'+ entityForName:失败报告

时间:2011-01-31 12:00:49

标签: objective-c xcode ios core-data nsfetchedresultscontroller

我的代码看起来像这样:

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
        NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

        // If appropriate, configure the new managed object.
        [newManagedObject setValue:[NSDate date] forKey:@"date"];

           // Save the context.
            NSError *error = nil;
            if (![context save:&error]) { 
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
        }

.h头文件中我有这个:

@interface TretiViewController : UIViewController <NSFetchedResultsControllerDelegate> {

 NSFetchedResultsController *fetchedResultsController_;
    NSManagedObjectContext *managedObjectContext_;

 // private NSString *accountID;
}


@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

它应该使用核心数据保存数据,但它会出错:

NavTest[1372:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Obrat''
*** Call stack at first throw:
(
 0   CoreFoundation                      0x00f92be9 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x010e75c2 objc_exception_throw + 47
 2   CoreData                            0x00cbf45b +[NSEntityDescription entityForName:inManagedObjectContext:] + 187
 3   NavTest                             0x0000c210 -[TretiViewController fetchedResultsController] + 187
 4   NavTest                             0x00007a76 -[TretiViewController ukazObrat0] + 38
 5   UIKit                               0x002c4a6e -[UIApplication sendAction:to:from:forEvent:] + 119
 6   UIKit                               0x003531b5 -[UIControl sendAction:to:forEvent:] + 67
 7   UIKit                               0x00355647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
 8   UIKit                               0x00354438 -[UIControl touchesBegan:withEvent:] + 277
 9   UIKit                               0x0054f987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
 10  UIKit                               0x005500fc _UIGestureRecognizerUpdateObserver + 927
 11  CoreFoundation                      0x00f73fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
 12  CoreFoundation                      0x00f090e7 __CFRunLoopDoObservers + 295
 13  CoreFoundation                      0x00ed1bd7 __CFRunLoopRun + 1575
 14  CoreFoundation                      0x00ed1240 CFRunLoopRunSpecific + 208
 15  CoreFoundation                      0x00ed1161 CFRunLoopRunInMode + 97
 16  GraphicsServices                    0x018c7268 GSEventRunModal + 217
 17  GraphicsServices                    0x018c732d GSEventRun + 115
 18  UIKit                               0x002d342e UIApplicationMain + 1160
 19  NavTest                             0x00002594 main + 102
 20  NavTest                             0x00002525 start + 53
)
NavTest.xcdatamodel中的

我有两个默认的角色{0}和attributes

Evet

我错过了什么?

2 个答案:

答案 0 :(得分:0)

您说您有一个名为"Obrta"的实体,但代码正在创建"Obrat"类型的对象。修复拼写错误应解决您的问题。

答案 1 :(得分:0)

更改fetchedResultsController中的代码:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrat" inManagedObjectContext:self.managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Obrta" inManagedObjectContext:self.managedObjectContext];
您使用Xcode创建的

Entity named必须与代码中的NSEntityDescription保持一致