0

我有三个实体:

  • Notification
  • PlanDate
  • User

关系:

  • Notification有一对一的关系PlanDate
  • Notification也有to-many关系User

代码:

NSLog (@"Selected Object From  Detail ViewDidLoad %@", managedObject);
NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);        
NSLog(@"planDateObject %@", planDateObject);
NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];
NSLog(@"recipientUserName: %@", recipientUserName);

这是日志:

2013-08-21 12:26:50.349 Time[5018:c07] Selected Object From  Detail ViewDidLoad <Notification: 0xa58ee70> (entity: Notification; id: 0xb2a5480 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p0B2ABAC1-77F3-4F46-B14D-34652F148B37> ; data: {
appType = 1;
invitationType = PlanDate;
lastmoddate = "2013-08-21 17:42:42 +0000";
"notification_id" = "0B2ABAC1-77F3-4F46-B14D-34652F148B37";
plandate = "0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7>";
users = "<relationship fault: 0xb2aa210 'users'>";
})
2013-08-21 12:26:50.350 Time[5018:c07] planDateObject <Notification: 0xb292800> (entity: Notification; id: 0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7> ; data: <fault>)
2013-08-21 12:26:53.406 Time[5018:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Notification 0xb292800> valueForUndefinedKey:]: the entity Notification is not key value coding-compliant for the key "recipientUserName".'

确实有一个带有值的属性“recipientUserName”。我已经尝试过产生相同日志的其他属性。

为什么会出错?为什么当我尝试访问其属性时数据显示为错误?

编辑

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

recipientUserName错误消息说实体上实际上没有命名属性Notification。让我们通过你的结果来看看发生了什么。首先你这样做:

NSLog (@"Selected Object From  Detail ViewDidLoad %@", managedObject);

结果是这样的:

2013-08-21 12:26:50.349 Time[5018:c07] Selected Object From  Detail ViewDidLoad <Notification: 0xa58ee70> (entity: Notification; id: 0xb2a5480 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p0B2ABAC1-77F3-4F46-B14D-34652F148B37> ; data: {
appType = 1;
invitationType = PlanDate;
lastmoddate = "2013-08-21 17:42:42 +0000";
"notification_id" = "0B2ABAC1-77F3-4F46-B14D-34652F148B37";
plandate = "0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7>";
users = "<relationship fault: 0xb2aa210 'users'>";
})

这表示 thatmanagedObject是 的一个实例Notification,并且 thatNotification的属性是:

  • appType
  • invitationType
  • lastmoddate
  • notification_id
  • plandate
  • users

plandate属性实际上是一对一的关系。根据日志消息,该关系另一端的对象是另一个实例Notification(由x-coredata托管对象 ID 表示)。我猜你的意思是这是一个实例PlanDate,但你的日志消息说这不是你实际拥有的。

接下来你这样做:

NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);        
NSLog(@"planDateObject %@", planDateObject);

结果是:

2013-08-21 12:26:50.350 Time[5018:c07] planDateObject <Notification: 0xb292800> (entity: Notification; id: 0xa1c9350 <x-coredata://C0FB76AD-19EB-42BA-981A-F99DD6DCF6C7-5018-0000101A36CFA3D1/Notification/p1C004B2B-F1DA-4EE0-9FAC-0A89E0DBCDB7> ; data: <fault>)

因此,如果您对什么类型有任何疑问planDateObject,这可以确定。它绝对是Notification实体的一个实例。类型转换到PlanDate这里没有任何意义。数据在这里显示<fault>是正常的,因为此时您还没有尝试访问它的任何属性。

最后你这样做:

NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];

您正在尝试获取recipientUserName属性的值。但是我们已经知道它planDateObject是 a Notification,并且它Notification没有具有该名称的属性。因此,您尝试访问不存在的密钥时会出现异常。

因此,如果您的PlanDate实体有一个名为 的属性recipientUserName,那么您的问题是plandate关系指向错误的对象。如果PlanDate没有该属性,则您有更复杂的问题无法从您提供的信息中解决。

于 2013-08-21T19:59:18.620 回答