Skip to content

Instantly share code, notes, and snippets.

@thisandagain
Created September 18, 2012 20:11
Show Gist options
  • Save thisandagain/3745555 to your computer and use it in GitHub Desktop.
Save thisandagain/3745555 to your computer and use it in GitHub Desktop.
Dictionary to properties for class init
#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