user1107173
user1107173

Reputation: 10764

CoreData: Accessing one-to-one relationship attribute

Two entities:

Notification has a one-to-one relationship with User called "senderUser". In NSManagedObject file for Notification, senderUser looks like this

@property (nonatomic, retain) User *senderUser;

Sender user has an NSString property called username

What's the correct syntax to access the property? I have tried the following and I'm getting an error:

Notification *managedObject = [array objectAtIndex:indexPath.row];
NSString *senderUN = [managedObject valueForKey:@"senderUser.username"];

Error:

 `*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Notification 0xbc4ad80> valueForUndefinedKey:]: the entity Notification is not key value coding-compliant for the key "senderUser.username"`.'

Upvotes: 0

Views: 122

Answers (1)

Martin R
Martin R

Reputation: 539955

You got it almost right:

NSString *senderUN = [managedObject valueForKeyPath:@"senderUser.username"];

because "senderUser.username" is not a single key, but a key path with two components.

Upvotes: 2

Related Questions