Created
September 18, 2012 20:11
-
-
Save thisandagain/3745555 to your computer and use it in GitHub Desktop.
Dictionary to properties for class init
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/NSObjCRuntime.h> | |
#import <objc/runtime.h> | |
#import "Underscore.h" | |
- (id)initWithDict:(NSDictionary *)dict | |
{ | |
self = [super init]; | |
if (self) { | |
// Storage object | |
NSMutableArray *propertyList; | |
// Property list | |
unsigned int outCount, i; | |
objc_property_t *properties = class_copyPropertyList([self class], &outCount); | |
for (i = 0; i < outCount; i++) { | |
objc_property_t property = properties[i]; | |
[propertyList addObject:[NSString stringWithUTF8String:property_getName(property)]]; | |
} | |
// Dictionary to properties | |
_dict(dict) | |
.pick(propertyList) | |
.each(^(id key, id obj) { | |
[self setValue:obj forKey:key]; | |
}); | |
} | |
return self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment