CoreData:访问一对一关系属性

时间:2013-09-09 20:43:01

标签: core-data attributes entity

两个实体:

  • 通知
  • 用户

Notification与用户名为“relationship”的一对一senderUser。在Notification的NSManagedObject文件中,senderUser看起来像这样

@property (nonatomic, retain) User *senderUser;

发件人用户有一个名为username

的NSString属性

访问该属性的正确语法是什么?我尝试了以下内容,但我收到了一个错误:

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

错误:

 `*** 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"`.'

1 个答案:

答案 0 :(得分:2)

你几乎是对的:

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

因为“senderUser.username”不是单个键,而是具有两个键的键路径 组件。

相关问题