美文网首页
iOS runtime 动态获取属性存放在数组

iOS runtime 动态获取属性存放在数组

作者: 闹钟先生的闹钟 | 来源:发表于2017-03-10 17:37 被阅读25次

    需要 #import <objc/runtime.h>

    实现

        NSMutableArray *allNames = [[NSMutableArray alloc] init];
        
        ///存储属性的个数
        unsigned int propertyCount = 0;
        
        ///通过运行时获取当前类的属性
        objc_property_t *propertys = class_copyPropertyList([self class], &propertyCount);
        
        //把属性放到数组中
        for (int i = 0; i < propertyCount; i ++) {
            ///取出第一个属性
            objc_property_t property = propertys[i];
            
            const char * propertyName = property_getName(property);
            
            [allNames addObject:[NSString stringWithUTF8String:propertyName]];
        }
    
    

    相关文章

      网友评论

          本文标题:iOS runtime 动态获取属性存放在数组

          本文链接:https://www.haomeiwen.com/subject/cnlogttx.html