码迷,mamicode.com
首页 > 其他好文 > 详细

利用runtime解析model

时间:2015-07-31 16:22:14      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:ios

利用runtime解析model

by 伍雪颖

+ (instancetype)modelWithDictionary: (NSDictionary *) data{
   
return [[self alloc] initWithDictionary:data];
}

- (
instancetype)initWithDictionary: (NSDictionary *) data{
    {
       
self = [super init];
       
if (self) {
            [
self assginToPropertyWithDictionary:data];
        }
       
return self;
    }
}

-(
void) assginToPropertyWithDictionary: (NSDictionary *) data{
   
if (data == nil) {
       
return;
    }
   
NSArray *dicKey = [data allKeys];
   
for (int i = 0; i < dicKey.count; i ++) {
       
SEL setSel = [self creatSetterWithPropertyName:dicKey[i]];
       
       
if ([self respondsToSelector:setSel]) {
           
NSString  *value = [NSString stringWithFormat:@"%@", data[dicKey[i]]];
            [
self performSelectorOnMainThread:setSel
                                  
withObject:value
                               
waitUntilDone:[NSThread isMainThread]];
        }
    }
}

- (
SEL) creatSetterWithPropertyName: (NSString *) propertyName{
    propertyName = propertyName.
capitalizedString;
    propertyName = [
NSString stringWithFormat:@"set%@:", propertyName];
   
return NSSelectorFromString(propertyName);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

利用runtime解析model

标签:ios

原文地址:http://blog.csdn.net/rainlesvio/article/details/47170261

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!