Last Updated: February 25, 2016
·
1.172K
· JuanKaram

Class Introspection

Sometimes when you are working with a 3rd party libraries or maintaining another developer code you need to know the methods and properties of the classes. When this happen the most common thing to do is open the class .h and look it at it, but this process is high time consuming.

It will be great to just type in the debugging console po [self inspect] and the console print all the ivars, properties and methods, right?

The way to achieve this is creating a category of NSObject, write the inspect method using the Objective C runtime to extract all of the values:

#import <objc/runtime.h>

- (NSString *)inspect
{
  Class objectClass = [self class];

  u_int count;


  Ivar *classIvars = class_copyIvarList(objectClass, &count);

  NSMutableArray *ivarList = [NSMutableArray arrayWithCapacity:count];

  for(int i = 0; i < count; i ++)
  {
    const char *ivarName = ivar_getName(classIvars[i]);

    [ivarList addObject:[NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding]];
  }

  free(classIvars);



  objc_property_t *classProperties = class_copyPropertyList(objectClass, &count);

  NSMutableArray *propertyList = [NSMutableArray arrayWithCapacity:count];

  for(int i = 0; i < count; i++)
  {
    const char *propertyName = property_getName(classProperties[i]);

    [propertyList addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
  }

  free(classProperties);



  Method *classMethods = class_copyMethodList(objectClass, &count);

  NSMutableArray *methodList = [NSMutableArray arrayWithCapacity:count];

  for(int i = 0; i < count; i++)
  {
    SEL methodNameSelector = method_getName(classMethods[i]);
    const char *methodName = sel_getName(methodNameSelector);

    [methodList addObject:[NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]];
  }

  free(classMethods);

  return [NSString stringWithFormat:@"Class: %@ \n Ivars: %@ \n Properties: %@ \n Methods: %@",
                                    NSStringFromClass([self class]),
                                    ivarList,
                                    propertyList,
                                    methodList];


}

To use it just import the category .h file and you are ready to go. Here is an example:

(NSString *) $1 = 0x098e4600 Class: UIWindow 
 Ivars: (
    "_delegate",
    "_windowLevel",
    "_windowSublevel",
    "_layerContext",
    "_lastMouseDownView",
    "_lastMouseEnteredView",
    "_firstResponder",
    "_fingerInfo",
    "_touchData",
    "_viewOrientation",
    "_exclusiveTouchView",
    "_undoManager",
    "_screen",
    "_transformLayer",
    "_rotationViewControllers",
    "_rootViewController",
    "_savedBackgroundColor",
    "_subtreeMonitoringViews",
    "_tintViews",
    "_currentTintView",
    "_windowFlags",
    "_windowController",
    "_windowInternalConstraints",
    "_rootViewConstraints",
    "_layoutEngine",
    "_deferredLaunchBlock",
    "__usesLegacySupportedOrientationChecks"
) 
 Properties: (
    screen,
    windowLevel,