Search code examples
objective-cruntimelanguage-designintrospection

Is it possible to determine what properties a class has programmatically?


Is it possible to determine what properties a class has programmatically? I want to see what properties a class (or subclass) has defined. I'm told this has to do with something called runtime introspection. I've found this article, but it seems to be just the tip of the iceberg.

So, is it possible to determine what properties a class has programmatically? How?

Edit:

I'm working on iOS, not Mac OS X. What headers do I need to import?


Solution

  • The Objective-C Runtime Programming Guide includes a section about listing properties. The relevant snippet that actually gets the list is:

    id LenderClass = objc_getClass("Lender");
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
    

    Then you use the runtime property functions to get whatever information you want about each property in the list.