1. 程式人生 > >Object-C 獲取物件的屬性和值

Object-C 獲取物件的屬性和值

@autoreleasepool {
        
        TestObj *obj = [[TestObj alloc]init];
        obj.age = @"22";
        obj.name = @"lisi";
        obj.gender = @"male";
        
        unsigned int count;
        objc_property_t *properties = class_copyPropertyList([obj class], &count);
        for(int i=0;i<count;i++){
            objc_property_t property = properties[i];
            NSString *key = [[NSString alloc]initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
            //kvc讀值
            NSString *value = [obj valueForKey:key];
            NSLog(@"%@->%@",key,value);
        }
    }