An object is not key for encoding a key for a 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 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]; 
+6
objective-c cocoa core-data
source share
6 answers

Good. Firstly, I would like to thank everyone who suggested the idea. They may not have helped me solve the problem, but I found out more about Core Data, and it is always useful to know that I should check when something is not working.

I really don't know what the problem is. Until this morning, I opened Xcode for about 5 days, and I think that yesterday I added the "bestTime" attribute to the data model. I can only assume that in 5 days Xcode became a little unstable and believed that it was saved when it was not. I checked that I retained the attributes of the model, in fact I had to check 3 or 4 times, as well as my habit of hitting Command + S after any change I make.

In any case, I rebooted my machine earlier today, and when I started Xcode a few minutes ago, I realized that "bestTime" was not in the model file. I added it, reset the settings on the iPad simulator, and it worked.

Thank you all for your help, sorry that the solution was not more interesting and based on code. Although it makes me feel better that my code was not the cause.

+14
source share

This managed entity does not have an attribute named " bestTime ". According to the exception message, it is definitely a puzzle, so you did not declare an attribute named bestTime in your model (or you saddened it or title it differently).

+11
source share

I decided to remove this problem and create the data model again, and then clean it up and then restore again. I think the error is caused by the underlying data, sometimes it does not update some data.

+2
source share

I do not think that there is enough information to determine the cause. You can try reading the Basic Data Troubleshooting Guide ; One possible reason could be if you initialized this particular instance of Puzzle using a simple init rather than initWithEntity.

+1
source share

If you added the bestTime attribute to the model at a later time, you may have forgotten to place the declaration and implementation for them in a related class of managed objects.

Try the convenient actions provided in Design β†’ Data Model β†’ Copy Objective-C ... Declarations / implementations of the method to the clipboard (when editing the model file).

+1
source share

If parsing JSON in a managed object, make sure you use the coreDataPropertyName property, not the json-key-name from JSON. Easy to mix when they are called that.

This error infuriated me, and all because I used image-url , but not imageURL .

0
source share

All Articles