Get properties of NSManagedObject as NSDictionary

There is a faster way to convert an NSManagedObject to an NSDictionary (from Matthias Bauch response at https://stackoverflow.com/a/5664745/2294824) :

NSArray *keys = [[[myObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];

You can achieve your approach by following method:

unsigned int count;
objc_property_t *properties = class_copyPropertyList([CoreDataObject class], &count);
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:16];

for(int i = 0; i < count; i++) {
    objc_property_t property = properties[i];
    NSString *name = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
    id obj = [coreDataObjectInstance valueForKey:name];
    if (obj) {
        // Skip properties with nil values (optionally you can use: [dictionary setObject:((obj == nil) ? [NSNull null] : obj) forKey:name]; without any if-statement) 
        [dictionary setObject:obj forKey:name];
    }
}

free(properties);

"CoreDataObject" is the core data object you would like to convert into a NSDictionary while "coreDataObjectInstance" is an instance of this core data object. Keep in mind you have to:

#include <objc/runtime.h>

Furthermore, it would be great if you can give us some more insights what you would like to achieve with this idea, maybe there is a different/better solution.

Hope this helps!


I think this is what you're actually looking for:

NSEntityDescription *trackEntity = [NSEntityDescription entityForName:[Track entityName] inManagedObjectContext:yourMOC];

then you have access to these dictionaries:

[trackEntity propertiesByName];
[trackEntity relationshipsByName];
[trackEntity attributesByName];

each of these dictionaries will have keys corresponding to the property, and the value will be an NSPropertyDescription.