您的位置:首页 > 移动开发 > IOS开发

iOS运行时应用场合二 获取对象属性

2015-09-07 16:01 441 查看
自定义一个model

@interface TestModel : NSObject
@property(nonatomic,strong)NSString* name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,strong)NSString* sex;
@end

遍历方法

 u_int count;
    objc_property_t *properties  =class_copyPropertyList([TestModel
class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray
arrayWithCapacity:count];
    for (int i =
0; i<count; i++)
    {
        const
char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString
stringWithUTF8String: propertyName]];
    }
    free(properties);
    NSLog(@"%@",propertiesArray);
    
    TestModel* model = [[TestModel
alloc] init];
    model.name =
@"苗苗";
    model.age =
22;
    model.sex =
@"female";
    
    for (int i =
0 ; i < propertiesArray.count; i ++ ) {
        NSLog(@"%@",[model
valueForKey:[propertiesArray
objectAtIndex:i]]);
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: