The entity User is not key value coding-compliant

2020-05-06 07:27发布

问题:

I have a swift project with 3 entities in my xcdatamodeld: Access, CustomerInfo and User. I am trying to save the dateEndSubscription separately in the User. When I am trying to save , I get error as : Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: the entity User is not key value coding-compliant for the key "dateEndSubscription".' Anyone please help to solve it in swift4.

回答1:

The json message contains the key dateEndSubscription but the attribute in your User entity is named dateEnd so they doesn’t match.

A few options to solve this in your saveUser method

Change API.DateEnd to dateEnd but maybe that will infer with the decoding of the json message.

Don't use API key but instead hardcode attribute name

 user.setValue(dateEnd, forKey: "dateEnd")

and lastly use the property of the User class directly

 user.dateEnd = dateEnd

You need to change saveCustomerInformation as well since you are working with a User object there as well although it's unclear why.