【发布时间】:2011-03-26 14:14:15
【问题描述】:
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 来存储关卡。当一个关卡完成并获胜时,我想为该关卡设置最佳时间。计算所用时间,如果比之前的最佳时间更好,我想将其设置为关卡的最佳时间。
此代码在尝试从 self.puzzle 检索最佳时间时在“int bestTime”行失败,self.puzzle 是来自 Core Data 的 NSManagedObject。最佳时间在核心数据模型中存储为整数 32。它失败并出现 SIGABRT 错误。
'[<NSManagedObject 0x95334d0> valueForUndefinedKey:]: the entity Puzzle is not key value coding-compliant for the key "bestTime".'
我已经在网上搜索了为什么会发生这种情况以及如何解决它,但似乎没有任何帮助。在其他地方,我可以从 Core Data 模型中访问整数值,它们运行良好,尽管它们用于过滤和排序查询。
我也不知道我设置值的那一行行不行。
对此的任何帮助将不胜感激。
编辑:这是获取一组谜题的代码,其中一个被视为上述谜题。
// 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”是您后来添加到模型中的属性吗? IE。这可能是迁移问题吗?
-
我后来确实添加了它,但我从模拟器中删除了应用程序并重新安装了它。
-
我也重置了模拟器的内容和设置,还是不行。和以前一样的错误。
标签: objective-c cocoa core-data