iOS runtime 根据类名获取属性的类型和名称

最近想简单封装一下FMDB,简单实现根据模型创建表的功能。

又研究了下runtime,解析property_getAttributes即可获得属性类型名称等

@protocol Aprotocol <NSObject>


@end


typedef void(^TestBlock)(int age);

@interface Student : NSObject

@property(nonatomic, copy)NSString<Aprotocol> *name ;

@property(nonatomic, assign)int score;

@property(nonatomic,assign)CGFloat cgfloat ;

@property(nonatomic, strong)NSArray *arr;

@property(nonatomic, strong)NSNumber *number;

@property(nonatomic, assign)float   afloat;

@property(nonatomic, weak)id   anId;

@property(           strong)Student *stu;

@property(nonatomic, copy)TestBlock   thisBlock;

@property BOOL aBool;  //TB,V_aBool

@property char charDefault;

@property(retain)id idRetain;

@property CGRect rect ;

@end


[self anilistMyClass:objc_getClass("Student")];


- (void)anilistMyClass:(Class)className{

    u_int count;

    objc_property_t * properties  = class_copyPropertyList(className, &count);

    for (int i=0; i<count; i++) {

        objc_property_t property = properties[i];

        NSLog(@"%@-->%@",getPropertyType(property),getPropertyName(property));

    }

    free(properties);

}


得到如下结果:

2015-09-20 10:22:55.130 RuntimeSample[6589:353450] NSString-->name

2015-09-20 10:22:55.130 RuntimeSample[6589:353450] int-->score

2015-09-20 10:22:55.131 RuntimeSample[6589:353450] double-->cgfloat

2015-09-20 10:22:55.131 RuntimeSample[6589:353450] NSArray-->arr

2015-09-20 10:22:55.131 RuntimeSample[6589:353450] NSNumber-->number

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] float-->afloat

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] id-->anId

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] Student-->stu

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] Block-->thisBlock

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] BOOL-->aBool

2015-09-20 10:22:55.132 RuntimeSample[6589:353450] char-->charDefault

2015-09-20 10:22:55.133 RuntimeSample[6589:353450] id-->idRetain

2015-09-20 10:22:55.133 RuntimeSample[6589:353450] CGRect-->rect



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值