Skip to content

Instantly share code, notes, and snippets.

@ramakser
Created February 5, 2016 12:32
Show Gist options
  • Save ramakser/89c70d8ea0ef1512db61 to your computer and use it in GitHub Desktop.
Save ramakser/89c70d8ea0ef1512db61 to your computer and use it in GitHub Desktop.
- (NSDictionary *)dictionaryRepresentation {
unsigned int count = 0;
// Get a list of all properties in the class.
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:count];
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
NSString *value = [self valueForKey:key];
// Only add to the NSDictionary if it's not nil.
if (value)
[dictionary setObject:value forKey:key];
}
free(properties);
return dictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment