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);

Objective-C // Runtime Method Injection

While building our Actionscript Native Extensions we've had to learn a lot about Objective C for the iOS versions. One trick that we'd like to share is how to inject a method into an instance of a class at runtime. This is necessary for several of the extensions we've done and it took me a while to figure out how to do this so I thought I'd share.

Quick Links:

Continue reading Objective-C // Runtime Method Injection