I have three entities:
NotificationPlanDateUser
Relationships:
Notificationhas to-one relationship withPlanDateNotificationalso has to-many relationship withUser
Code:
NSLog (@"Selected Object From Detail ViewDidLoad %@", managedObject);
NSManagedObject *planDateObject = ((PlanDate *)[managedObject valueForKey:@"plandate"]);
NSLog(@"planDateObject %@", planDateObject);
NSString *recipientUserName = [planDateObject valueForKey:@"recipientUserName"];
NSLog(@"recipientUserName: %@", recipientUserName);
Here is the log:
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".'
There is indeed an attribute "recipientUserName" with a Value. I have tried other attributes that produce the same log.
Why the error? Why is the Data shown as fault when I'm trying to access its attribute?
EDIT


The error message says that there is not actually an attribute named
recipientUserNameon the entityNotification. Let's run through your results to see what happened. First you do this:And the result is this:
This says that
managedObjectis an instance ofNotification, and thatNotification's attributes are:appTypeinvitationTypelastmoddatenotification_idplandateusersThe
plandateattribute is actually a to-one relationship. According to the log message, the object at the other end of this relationship is another instance ofNotification(as indicated by thex-coredatamanaged object ID representation). I'm guessing you meant this to be an instance ofPlanDate, but your log message says that this isn't what you actually have there.Next you do this:
And the result is:
So if you had any doubt about what type
planDateObjectis, this nails it. It's most definitely an instance of theNotificationentity. The typecast toPlanDatedoesn't mean anything here. The fact that the data is shown as<fault>is normal here, because at this point you haven't tried to access any of its attributes.Finally you do this:
You're trying to get the value of the
recipientUserNameattribute. But we already know thatplanDateObjectis aNotification, and thatNotificationdoes not have an attribute with that name. So, you get an exception from trying to access a nonexistent key.So, if your
PlanDateentity has an attribute namedrecipientUserName, then your problem is that theplandaterelationship is pointing at the wrong object. IfPlanDatedoes not have that attribute, you have more complex problems that can't be solved from the information you've provided.