#import <Foundation/Foundation.h>
@interface NSObject (PropertyList)
+ (instancetype)objWithDict:(NSDictionary *)dict;
@end
#import "NSObject+PropertyList.h"
#import <objc/runtime.h>
@implementation NSObject (PropertyList)
+ (instancetype)objWithDict:(NSDictionary *)dict {
id instance = [[self alloc]init];
[[self propertys] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
id value = dict[obj];
if (value) {
[instance setValue:value forKey:obj];
}
}];
return instance;
}
+ (NSArray *)propertys {
unsigned int count;
objc_property_t *list = class_copyPropertyList(self, &count);
NSMutableArray *data = [NSMutableArray array];
for (int i = 0; i < count; ++i) {
objc_property_t key = list[i];
NSString *propertyName = [[NSString alloc]initWithCString:property_getName(key) encoding:NSUTF8StringEncoding];
[data addObject:propertyName];
}
free(list);
return data.copy;
}
@end