实体不是键的键值编码兼容 [英] Entity is not key value coding-compliant for the key

查看:105
本文介绍了实体不是键的键值编码兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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"]);
    }
}

这是来自我正在制作的iPad游戏的一些代码并且我使用Core Data来存储级别。当一个关卡完成并赢了,我想为该关卡设置最佳时间。计算所需的时间,如果它比以前的最佳时间更好,我想将其设置为该级别的最佳时间。

This is some code from an iPad game I am making and I am using Core Data for storing the levels. 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.

当试图从Core数据中的self.puzzle(一个NSManagedObject)检索最佳时间时,此代码在int bestTime行失败。最佳时间以核心数据模型中的整数32存储。它会失败并出现SIGABRT错误。

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. The best time is stored as an Integer 32 in the Core Data model. It fails with a SIGABRT error.

'[<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.

我也不知道如果我设置值的行将工作。

I also don't know if the line where I set the value will work.

任何帮助将非常感激。

编辑:这是获取一个谜题数组的代码,其中一个被认为是上面的谜题。

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.

我真的不知道什么问题。直到今天上午,我有Xcode打开大约5天我想和昨天我添加了属性'bestTime'到数据模型。我只能假设在5天,Xcode已经变得有点不稳定,并认为它被保存时,它不是。我检查了我保存了模型属性,事实上,我必须检查3或4次,以及我的任何更改后,我按下Command + S的习惯。

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. I can only assume that over the 5 days, Xcode had become a little unstable and thought it was saved when it wasn't. 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.

无论如何,我重新启动了我的机器今天早些时候,当我启动Xcode几分钟前我意识到'bestTime'不是在模型文件。我添加了它,重置iPad模拟器上的设置,它工作。

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. I added it, reset the settings on the iPad simulator and it worked.

再次感谢大家的帮助,对不起解决方案不是更有趣和基于代码。虽然它让我感觉更好,我的代码不是原因。

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.

这篇关于实体不是键的键值编码兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆