Skip to content

Instantly share code, notes, and snippets.

@modamoda
Last active August 29, 2015 14:06
Show Gist options
  • Save modamoda/a3d7a67e6e8dab67e491 to your computer and use it in GitHub Desktop.
Save modamoda/a3d7a67e6e8dab67e491 to your computer and use it in GitHub Desktop.
Make dictionary of properties in objective c
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface DictionaryConvertable : NSObject
- (NSDictionary *)toDictionary;
@end
// ----------
@implementation DictionaryConvertable
- (NSDictionary *)toDictionary
{
NSMutableArray *propertyNames = [NSMutableArray array];
id currentClass = [self class];
NSString *propertyName;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
propertyName = [NSString stringWithUTF8String:property_getName(property)];
[propertyNames addObject:propertyName];
}
NSDictionary *propertyDict = [self dictionaryWithValuesForKeys:propertyNames];
free(properties);
return propertyDict;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment