Reflection

December 16th, 2010
#reflection

To retrieve the properties for an object, you use:

unsigned int outCount;
objc_property_t *propList = class_copyPropertyList([objectToInspect class], &outCount);

propList will contain an array of the properties found inside objectToInspect, with outCount containing the total number of properties.

To retrieve the properties name you would:

NSString *propName = [NSString stringWithUTF8String:property_getName(propList[indexOfWantedElement)]];

To get the attributes of the property you would:

NSString *attrs = [NSString stringWithUTF8String:property_getAttributes(propList[indexOfWantedElement)]];

A detailed account can be found in the Objective-C documentation

What do you think? Let me know by getting in touch on Twitter - @wibosco