获取对象的属性名、属性值、属性特性,协议列表

5 篇文章 0 订阅
unsigned int count = 0;
//获取属性名
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    for (int i =0; i < count; i ++) {
        const char *propertyName =property_getName(properties[i]);//获取属性名
        constchar *propertyAttr =property_getAttributes(properties[i]);//获取属性特性  通常是T@"类型"开头,V_属性名称结尾的格式   可以搜索苹果文档“Property Attribute Description Examples”
        id propertyValue = [selfvalueForKey:[NSStringstringWithUTF8String:propertyName]];//获取属性名对应的值,如果是非对象数据返回NSValue类型
        NSLog(@"name:%@,attr:%@,value:%@",[NSStringstringWithUTF8String:propertyName],[NSStringstringWithUTF8String:propertyAttr],propertyValue);
    }
free(properties);//这里要对取出来的指针数组进行释放,不然会有内存泄露
//获取对象中的变量名
Ivar *vars = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i ++) {
     const char *ivarName = ivar_getName(vars[i]);
     const char *ivarType = ivar_getTypeEncoding(vars[i]);
} 
free(vars);//同样要释放
//获取协议列表
__unsafe_unretained Protocol ** proList= class_copyProtocolList([TestObject class], &count);
    for (int i = 0;  i < count; i ++) {
        Protocol *pro = proList[i];
        const char *name = protocol_getName(pro);//协议名称
        NSLog(@"%@",[NSString stringWithUTF8String:name]);
    }
    free(proList);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值