The Shop object does not match the key value for the key "category.name",

I am developing an iOS application with CoreData.

I have two objects:

Score

enter image description here

Category

enter image description here

I try to access category.name from Shop , but I get an error:

 - (void)updateDetails:(NSManagedObject *)shop { NSLog(@"updateDetails: %@", shop); if (shop == nil) return; self.nameLabel.text = [[shop valueForKey:@"name"] description]; self.categoryLabel.text = [[shop valueForKey:@"category.name"] description]; self.addressLabel.text = [[shop valueForKey:@"address"] description]; self.telephoneLabel.text = [[shop valueForKey:@"telephone"] description]; NSNumberFormatter* f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber* acceptRate = [f numberFromString:[[shop valueForKey:@"acceptRate"] description]]; _ratingControl.rating = [acceptRate unsignedIntValue]; } 

I get Shop objects this way:

 NSManagedObjectContext *context = [self managedObjectContext]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Shop" inManagedObjectContext:context]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:entity]; NSArray *results = [context executeFetchRequest:fetchRequest error:nil]; 

But I get this error:

'[<NSManagedObject 0x1cdb4890> valueForUndefinedKey:]: the entity Shop is not key value coding-compliant for the key "category.name".'

How can I solve this error?

+4
source share
1 answer

self.categoryLabel.text = [[shop valueForKey:@"category.name"] description];

it should be

self.categoryLabel.text = [[shop valueForKeyPath:@"category.name"] description];

Reason . From Key Value Encoding Documentation

A key is a string that identifies a specific property of an object. Typically, the key matches the name of the access method or instance variable in the receiving object. Keys must use ASCII encoding, begin with a lowercase letter and not contain spaces.

Some examples of keys would be payee , openingBalance , transactions and amount .

The key path is a line of dot-separated keys, which is used to indicate the sequence of properties of an object to move. The property of the first key in the sequence refers to the receiver, and each subsequent key is evaluated relative to the value of the previous property.

For example, the key path address.street will receive the value of the address property from the receiving object, and then determine the street property relative to the address object.

+7
source

All Articles