iOS的Objective C中的属性反射

big*_*can 4 objective-c ios

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

我现在正在为类做类似的事情

NSString *str = NSStringFromClass([object class]);  
Run Code Online (Sandbox Code Playgroud)

我想要做的是使用这个结果返回到类,看看哪些属性可用,以及这些属性是什么类型.

MrW*_*med 8

可能会有所帮助:

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

objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)
Run Code Online (Sandbox Code Playgroud)

然后从每个属性中,您可以使用property_getName函数获取属性名称,使用该函数获取属性属性property_getAttributes(如果需要过滤读写属性).

  • 查看[Objective-C运行时编程指南](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048 -CH101-SW24)和[Objective-C运行时参考](https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html)获取更多信息. (2认同)