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]]);
}
@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]]);
}
相关文章推荐
- iOS应用崩溃日志分析
- IOS CoreText -- 基本用法
- iOS路径获取
- iOS运行时场合一 category中添加(连接)对象(fake机制)
- iOS项目部分-01(注册)-使用正则表达式判断用户输入的是不是手机号码
- BIOS的基本概念和理解
- IOS开发指南学习——视图与控件
- 自学ios开发学习路线详细分解
- 使用Cardme读取安卓IOS导出的vcf格式通讯录
- ios时间处理
- iOS-多线程编程
- iOS开发——实现TODO宏定义
- ios开发系列之label自适应大小
- ios开发 Xcode下安装Versions以及Xcode配置svn
- iOS开发摇一摇功能
- iOS开发 - Keychain共享
- megapix-image.js使用的一个坑-ios图片裁剪之画布绘制大图片
- ios开发系列之根据字典创建模型
- iOS小结(二)navigation
- IOS动画Core Animation详解