Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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];
like image 589
danpalmer Avatar asked Aug 06 '10 22:08

danpalmer


1 Answers

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.

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.

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.

like image 72
danpalmer Avatar answered Sep 18 '22 15:09

danpalmer