Objective-C // List methods of a class

This simple Objective-C code excerpt prints the methods and arguments of a class instance using the NSLog functionality.

  1. //
  2. // List the methods of the class instance "myClass"
  3. methods = class_copyMethodList([myClass class], &methodCount);
  4. for (int i=0; i<methodCount; i++)
  5. {
  6. char buffer[256];
  7. SEL name = method_getName(methods[i]);
  8. NSLog(@"Method: %@", NSStringFromSelector(name));
  9. char *returnType = method_copyReturnType(methods[i]);
  10. NSLog(@"The return type is %s", returnType);
  11. free(returnType);
  12. // self, _cmd + any others
  13. unsigned int numberOfArguments = method_getNumberOfArguments(methods[i]);
  14. for(int j=0; j<numberOfArguments; j++)
  15. {
  16. method_getArgumentType(methods[i], j, buffer, 256);
  17. NSLog(@"The type of argument %d is %s", j, buffer);
  18. }
  19. }
  20. free(methods);

Published by

Michael

http://michaelarchbold.com

Leave a Reply

Your email address will not be published. Required fields are marked *