Last active
August 29, 2015 14:06
-
-
Save modamoda/a3d7a67e6e8dab67e491 to your computer and use it in GitHub Desktop.
Make dictionary of properties in objective c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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