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 kind of code from an iPad game that I make, and I use Core Data to store levels. When the level is completed and won, I want to set the best time for this level. The selected time 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 crashes in the string "int bestTime" when it tries to extract the best time from self.puzzle, which is NSManagedObject from Core Data. The best time is stored as Integer 32 in the Core Data model. Error with SIGABRT error.
'[<NSManagedObject 0x95334d0> valueForUndefinedKey:]: the entity Puzzle is not key value coding-compliant for the key "bestTime".'
I searched online for reasons why this is happening and how to fix it, but nothing helped. There are other places where I access Integer values ββfrom the Core Data model, and they work fine, although they are used to filter and sort queries.
I also don't know if the line in which I set the value will work.
Any help on this would be greatly appreciated.
EDIT: this is the code that extracts an array of puzzles, of which one is considered the puzzle above.
// 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];
objective-c cocoa core-data
danpalmer
source share