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游戏,我做了一些代码,我使用的核心数据存储水平。当一个级别完成并获胜时,我想设置该级别的最佳时间。所花费的时间是计算出来的,如果它比以前的最佳时间好,我想把它设置为关卡的最佳时间。实体不是键值编码兼容的关键
当它尝试从self.puzzle(它是来自Core Data的NSManagedObject)检索最佳时间时,此代码在'int bestTime'行上失败。最佳时间以核心数据模型中的整数32存储。它失败并出现SIGABRT错误。
'[<NSManagedObject 0x95334d0> valueForUndefinedKey:]: the entity Puzzle is not key value coding-compliant for the key "bestTime".'
我在网上搜索了原因,为什么发生这种情况以及如何解决它,但似乎没有任何帮助。还有其他地方可以从Core Data模型中访问Integer值,并且它们可以很好地工作,尽管它们用于过滤和排序查询。
我也不知道如果我设置值的行将工作。
任何帮助,将不胜感激。
编辑:这是获取其中一个被视为上述难题的谜题数组的代码。
// 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];
'bestTime'是您之后添加到模型中的属性吗?即这可能是一个迁移问题吗? – 2010-08-06 23:39:08
我以后再添加它,但我从模拟器中删除了应用程序并重新安装了它。 – danpalmer 2010-08-06 23:45:02
我也重置了模拟器的内容和设置,但它仍然不起作用。与以前一样的错误。 – danpalmer 2010-08-06 23:47:11