通过runtime获取一个类的所有属性

来源:互联网 发布:应用数据可以删吗 编辑:程序博客网 时间:2024/05/22 15:51

通过runtime获取一个类的所有属性,

- (NSArray *) attributeArray {    uint propertyCount;    objc_property_t *ps = class_copyPropertyList([MyObject class], &propertyCount);    NSMutableArray* results = [NSMutableArray arrayWithCapacity:propertyCount];    for (uint i = 0; i < propertyCount; i++) {        objc_property_t property = ps[i];        const char *propertyName = property_getName(property);        const char *propertyAttributes = property_getAttributes(property);        NSString* type = [NSString stringWithUTF8String:propertyAttributes];        type = [type componentsSeparatedByString:@"\""][1];        [results addObject:@{@"name":name, @"type":type}];    }    return results;}
0 0
原创粉丝点击