简体   繁体   English

实体不符合密钥的密钥值编码要求

[英]Entity is not key value coding-compliant for the key

    if (win) {
    // Game was won, set completed in puzzle and time
    // Calculate seconds taken
    int timeTaken = (int)([NSDate timeIntervalSinceReferenceDate] - self.gameStartTime);
    int bestTime = [[self.puzzle valueForKey:@"bestTime"] intValue];
    if (timeTaken < bestTime && bestTime != 0) {
        [self.puzzle setValue:[NSNumber numberWithInt:timeTaken] forKey:@"bestTime"];
        NSLog(@"Best time for %@ is %@", [self.puzzle valueForKey:@"name"], [self.puzzle valueForKey:@"bestTime"]);
    }
}

This is some code from an iPad game I am making and I am using Core Data for storing the levels. 这是我正在制作的iPad游戏中的一些代码,我正在使用Core Data来存储关卡。 When a level is completed and won, I want to set the best time for that level. 当一个关卡完成并赢得胜利后,我想为该关卡设定最佳时间。 The time taken is calculated, and if it is better than the previous best time, I want to set it as the best time for the level. 计算所需的时间,如果它比以前的最佳时间好,我想将其设置为该级别的最佳时间。

This code fails on the 'int bestTime' line when it tries to retrieve the best time from self.puzzle which is an NSManagedObject from Core Data. 当它尝试从self.puzzle检索最佳时间时,此代码在'int bestTime'行上失败,self.puzzle是Core Data中的NSManagedObject。 The best time is stored as an Integer 32 in the Core Data model. 最佳时间存储为Core Data模型中的Integer 32。 It fails with a SIGABRT error. 它因SIGABRT错误而失败。

'[<NSManagedObject 0x95334d0> valueForUndefinedKey:]: the entity Puzzle is not key value coding-compliant for the key "bestTime".'

I have searched online for reasons as to why this is happening and how to fix it, but nothing seems to have helped. 我已经在网上搜索了有关发生这种情况的原因以及解决方法,但似乎没有任何帮助。 There are other places where I access Integer values from the Core Data model and they work perfectly, although they are used to filter and sort queries. 在其他地方,我可以从Core Data模型中访问Integer值,尽管它们用于过滤和排序查询,但它们可以完美工作。

I also don't know if the line where I set the value will work. 我也不知道我设置值的行是否有效。

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。

EDIT: This is the code that fetches an array of puzzles of which one is taken to be the above puzzle. 编辑:这是获取一系列难题的代码,其中一个难题被视为上述难题。

// Define our table/entity to use  
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Puzzle" inManagedObjectContext:managedObjectContext];   

// Setup the fetch request  
NSFetchRequest *request = [[NSFetchRequest alloc] init];  
[request setEntity:entity];   

// Set the filter for just the difficulty we want
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"difficulty == %d", difficulty];
[request setPredicate:predicate];

// Define how we will sort the records  
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortid" ascending:YES];  
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];  
[request setSortDescriptors:sortDescriptors];  
[sortDescriptor release];

// Fetch the records and handle an error  
NSError *error;  
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

Ok, Firstly, I would like to thank everyone who suggested ideas. 好的,首先,我要感谢所有提出想法的人。 They may not have helped me solve the problem, but I learnt more about Core Data and it is always good to find out what I should be checking when things don't work. 他们可能没有帮助我解决问题,但是我了解了有关Core Data的更多信息,并且当事情不起作用时找出我应该检查的内容总是很有益的。

I don't really know what the problem was. 我真的不知道问题出在哪里。 Until this morning I had Xcode open for about 5 days I think and yesterday I added the attribute 'bestTime' to the data model. 直到今天早上,我认为Xcode的开放时间约为5天,昨天我在数据模型中添加了属性“ bestTime”。 I can only assume that over the 5 days, Xcode had become a little unstable and thought it was saved when it wasn't. 我只能假设在过去的5天中,Xcode变得有点不稳定,并认为它在未保存时已保存。 I had checked that I had saved the model attributes, in fact I must have checked 3 or 4 times as well as my habit of hitting Command+S after any change I make. 我检查了是否保存了模型属性,实际上,我必须检查3或4次,以及进行任何更改后按Command + S的习惯。

Anyway, I rebooted my machine earlier today and when I started up Xcode a few minutes ago I realised that 'bestTime' was not in the model file. 无论如何,我今天早些时候重新启动了机器,几分钟前启动Xcode时,我意识到'bestTime'不在模型文件中。 I added it, reset the settings on the iPad simulator and it worked. 我添加了它,重置了iPad模拟器上的设置,它开始工作了。

Thank you all again for the help, sorry the solution wasn't more interesting and code based. 再次感谢大家的帮助,对不起,该解决方案并不是更有趣且基于代码。 Although it makes me feel better that my code wasn't the cause. 尽管让我感觉不是代码的原因让我感觉更好。

That managed object doesn't have an attribute named “ bestTime ”. 该托管对象没有名为“ bestTime ”的属性。 According to the exception message, it definitely is a Puzzle, so you haven't declared an attribute named bestTime in your model (or you misspelled it or capitalized it differently). 根据异常消息,它肯定是一个难题,因此您没有在模型中声明一个名为bestTime的属性(或者您拼写了错误的名称或将其大写)。

I did solve the same problem by delete and create the data model again and clean then rebuild again. 我确实通过删除并再次创建数据模型并清理然后重新构建来解决了相同的问题。 I think the bug is caused by core data does not update some data inside sometimes. 我认为该错误是由核心数据引起的,有时内部不会更新某些数据。

I don't think there's enough information here to determine the cause. 我认为这里没有足够的信息来确定原因。 You might try reading the Core Data Troubleshooting Guide ; 您可以尝试阅读《 核心数据故障排除指南》 one possible cause could be if you initialized this particular instance of Puzzle using plain init rather than initWithEntity. 一个可能的原因是,如果您使用普通的init而不是initWithEntity初始化了这个Puzzle的特定实例。

If you added attribute bestTime to the model at the later time, you might have forgotten to put declaration and implementation for them in the connected Managed Object Class. 如果稍后将属性bestTime添加到模型中,则可能忘记将它们的声明和实现放在连接的受管对象类中。

Try convenience actions provided in Design -> Data Model -> Copy Objective-C ... Method Declarations/Implementations to Clipboard (when editing your Model file). 尝试“设计->数据模型->将Objective-C ...方法声明/实现复制到剪贴板”中提供的便捷操作(在编辑模型文件时)。

If parsing JSON into a managed object, be sure you're using the coreDataPropertyName property rather than the json-key-name key from JSON. 如果将JSON解析为托管对象,请确保使用的是coreDataPropertyName属性,而不是json-key-name键。 Easy to mix up when they're named so similarly. 如此命名时,很容易混淆。

This error was driving me nuts, and all because I was using image-url rather than imageURL . 这个错误使我发疯,所有的原因是我使用的是image-url而不是imageURL

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM