iOS的Objective C中的属性反射 [英] Property reflection in Objective C for iOS

查看:151
本文介绍了iOS的Objective C中的属性反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Objective C中的类上使用反射来确定运行时可用的属性。

I would like to be able to use reflection on a classes within Objective C to determine what properties are available at run time.

我为类做了类似的事情现在使用

I do something similar for classes right now using

NSString *str = NSStringFromClass([object class]);  

我想做的是使用这个结果返回课程,看看有哪些属性可用以及这些属性的类型。

What I would like to do is use this result to go back to the class and see what properties are available as well as what type these properties are.

推荐答案

可能会有所帮助:

您可以使用class_copyPropertyList获取类中的属性列表

You can get the list of properties in a class using class_copyPropertyList

objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)

然后从每个属性中,您可以使用<$ c $获取属性名称c> property_getName 使用 property_getAttributes 函数的函数和属性属性(如果需要过滤读写属性)。

and then from each property, you can get the property name using property_getName function and the property attributes using the property_getAttributes function (if you need to filter read-write properties).

这篇关于iOS的Objective C中的属性反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆