简体   繁体   English

实体xxx不符合键“(null)”的键值编码

[英]The entity xxx is not key value coding-compliant for the key “(null)”

I am trying to write a simple table view editor for a Core Data entity. 我正在尝试为Core Data实体编写一个简单的表视图编辑器。 Unfortunately I'm running into problems. 不幸的是我遇到了问题。

The error occurs when adding the very first entity to the table. 将第一个实体添加到表时,会发生错误。 The process for bringing up the modal dialog is as follows: 弹出模式对话框的过程如下:

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
    insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSManagedObject *newManagedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:context];

NameEditController *dialog = [[NameEditController alloc] init];
dialog.managedObject = newManagedObject;
[newManagedObject release];

UINavigationController *navCtrlr = [[UINavigationController alloc] initWithRootViewController:dialog];
navCtrlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[[self navigationController] presentModalViewController: navCtrlr animated:YES];    
[navCtrlr release];

Inside of NameEditController , I have this after the Done button is pressed: NameEditController内部,按下“完成”按钮后,我有了这个:

NSString* name = self.nameLabel.text;
[self.managedObject setValue:name forKey:@"name"];

NSError *error = nil;
if (![managedObject.managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
UIViewController *ctrl = [self parentViewController];
[ctrl dismissModalViewControllerAnimated:YES];

The very first time I create an object (when the list is empty) I get this: 第一次创建对象(列表为空)时,我得到以下信息:

Exception was caught during Core Data change processing: [ valueForUndefinedKey:]: the entity Group is not key value coding-compliant for the key "(null)". 在核心数据更改处理期间捕获了异常:[valueForUndefinedKey:]:实体Group不符合键“(null)”的编码标准。

If I fill out the 'name' field before bringing up the dialog, I am able to add the first entity successfully: 如果在弹出对话框之前填写“名称”字段,则可以成功添加第一个实体:

[newManagedObject setValue:@"New Group" forKey:@"name"]; [newManagedObject setValue:@“ New Group” forKey:@“名称”]; // this works //这有效

I am using NSFetchedResultsController to manage the table view BTW. 我正在使用NSFetchedResultsController管理表视图BTW。

Thanks! 谢谢!

In your first code block, the third line of code appears to be out of context. 在您的第一个代码块中,第三行代码似乎没有上下文。 Not sure if there's something there that could be contributing. 不知道那里是否有什么可以做的。

Secondly, the easiest way to get yourself an NSManagedObject into a NSManagedObjectContext from an entity name is to use the [NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:] selector. 其次,从实体名称中将NSManagedObject放入NSManagedObjectContext的最简单方法是使用[NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:]选择器。

So, I'd do something like: 所以,我会做类似的事情:

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityName:@"Group" inManagedObjectContext:context];

You won't need to release newObject anymore, since the [NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:] selector will return an object with a retain count of 0. Also, make sure that you have NameEditController specifying its managedObject property as retained. 您不再需要释放newObject,因为[NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:]选择器将返回保留计数为0的对象。此外,请确保您具有NameEditController,将其ManagedObject属性指定为保留。

To address your actual issue, it sounds like maybe you have 'name' specified as a required property in your data model? 为了解决您的实际问题,听起来好像您已将“名称”指定为数据模型中的必需属性? A screenshot showing the details for 'name' in your data model would help. 屏幕快照显示数据模型中“名称”的详细信息会有所帮助。

Yarr... sorry guys, it was actually in my didChangeObject:atIndexPath:forChangeType:newIndexPath: function hastily copied from elsewhere. Yarr ...对不起,这实际上是在我的didChangeObject:atIndexPath:forChangeType:newIndexPath:函数匆匆从其他地方复制的。 Apparently an exception thrown here can get obscured inside of the save: method. 显然,在这里抛出的异常会在save:方法内部被掩盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “此类不符合密钥的键值编码标准……” - “this class is not key value coding-compliant for the key…” NSObject类的键值编码兼容? - key value coding-compliant for NSObject class? 此类不是键值编码兼容的 - This class is not a key value coding-compliant 即使在重启我的开发Mac之后,实体仍然不是密钥的密钥值编码兼容 - Entity is still not key value coding-compliant for the key, even after rebooting my dev Mac Xcode“这个类不是密钥值编码兼容的关键页面控件。” - Xcode “this class is not key value coding-compliant for the key pageControl.” 应用程序崩溃,因为 - “类不是键的键值编码兼容” - App crashes because - "Class is not key value coding-compliant for the key" 获取错误类不是密钥值的密钥值编码兼容 - Getting error class is not key value coding-compliant for the key name “'NSUnknownKeyException',原因:…此类与键scoreArray的键值编码不兼容” - “'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key scoreArray ” Xcode这个类不是键视图的键值编码兼容 - Xcode this class is not key value coding-compliant for the key view 类别不符合键NavigationBar的键值编码标准 - class is not key value coding-compliant for the key navigationBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM