首页 > 其他 > 详细

使用runtime机制实现属性的赋值

时间:2017-03-01 16:19:47      阅读:296      评论:0      收藏:0      [点我收藏+]

- (void)encodeWithCoder:(NSCoder *)aCoder

{

    

    NSLog(@"%s",__func__);

    unsigned int count =  0;

    Class cls = self.class;

    Ivar * varList = NULL;

    objc_property_t * proList = NULL;

    while (cls!=[NSObject class])

    {

         BOOL isSelfClass = (cls == self.class);

        if(isSelfClass)

        {

           varList = class_copyIvarList(cls, &count);

            

        }else

        {

          proList = class_copyPropertyList(cls, &count);

        }

        for (int i = 0; i < count; i++)

        {

            const char * name =  isSelfClass?ivar_getName(varList[i]):property_getName(proList[i]);

            NSString * key = [NSString stringWithFormat:@"%s",name];

            NSArray * filters = @[@"superclass", @"description", @"debugDescription", @"hash"];

            if ([self valueForKey:key] && [filters containsObject:key] == NO) {

                [aCoder encodeObject:[self valueForKey:key] forKey:key];

            }

            

        }

        if(varList==NULL)

        {

             free(varList);

            varList = NULL;

        }else if (proList==NULL)

        {

            free(proList);

            proList = NULL;

            

        }

        cls = class_getSuperclass(cls);

        

    }

    

}

 

使用runtime机制实现属性的赋值

原文:http://www.cnblogs.com/VzhanggengD/p/6484587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!