获取对象的属性名、属性值、属性特性,协议列表
2016-02-17 11:37
295 查看
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);
相关文章推荐
- PHP正确的使用复数
- (笔记)电路设计(十四)之放大器的应用
- ActiveForm实例 大全
- Android开发:shape和selector和layer-list的(详细说明)
- 理解软件性能(2)
- 以太网帧格式
- 希尔排序
- github 初试
- iOS 设计模式之策略模式( Strategy)
- 2015工作总结以及2016期望目标
- iframe
- 2W/月和1W/月的工作,你会怎么选?
- 从输入URL到页面加载完成的过程中都发生了什么事情?
- APP调用系统相册,使用3DTouch重压,崩溃
- 高速缓冲存储器
- Android Studio的library项目依赖,Jar包,常用快捷键的对比
- 使用python实现rsa算法代码
- 汉字是如何转成十六进制的?
- 图像处理知识复习大纲
- android 主流浏览器对 scheme 打开本地 APP 功能的兼容性