11

What does this part of Apple core data documentation means?

User Info Dictionaries

Many of the elements in a managed object model—entities, attributes, and relationships—have an associated user info dictionary. You can put whatever information you want into a user info dictionary, as key-value pairs. Common information to put into the user info dictionary includes version details for an entity, and values used by the predicate for a fetched property.

I understand that by default entities have that dictionary, but I'm not able to find find userInfo on coredata entities or attributes.

Community
  • 1
  • 1
drasick
  • 280
  • 4
  • 14
  • 1
    This is a nice trick I found from [here](http://www.cimgf.com/2011/06/02/saving-json-to-core-data/#comment-1618): "This is pretty much the same thing I came up with mine, though I added a slight twist to mine. You can add custom keys to the userInfo dictionary for attributes in your object model, allowing you to customize the keyPath within the JSON data that you want to retrieve. This means the key name in Core Data can be different or can be the result of a deeply-nested property in the JSON data." – mfaani Dec 14 '18 at 20:46
  • "Doing it this way also lets you specify a custom selector for doing special formatting / processing of the incoming value, in case you need to do something more than dateFromString: or numberWithInteger, etc." – mfaani Dec 14 '18 at 20:46

1 Answers1

16

Get the NSEntityDescription from your NSManagedObject (through the entity property) or from your NSManagedObjectModel and get the NSAttributeDescription through attributesByName. This gives you a dictionary where you get the proper Description by name which has the userInfo as a property as well.

NSManagedObject *managedObject;
NSEntityDescription *entityDescription = managedObject.entity;
NSAttributeDescription *attributeDescription = entityDescription.attributesByName[@"someAttribute"];
NSString *foo = attributeDescription.userInfo[@"foo"];
Karl
  • 1,233
  • 8
  • 16
  • So that means `userInfo`s are available at the level of "classes" (i.e. entities and their constituents), not "instances" (i.e. `NSManagedObject`s), right? – Drux Mar 06 '15 at 09:43