Skip to content

Instantly share code, notes, and snippets.

@BennettSmith
Created January 14, 2012 22:27
Show Gist options
  • Save BennettSmith/1613121 to your computer and use it in GitHub Desktop.
Save BennettSmith/1613121 to your computer and use it in GitHub Desktop.
Objective-C class category to print out names of all selectors on a class.
//
// NSObject+DumpClassInfo.h
// DumpSelectors
//
// Created by Bennett Smith on 1/14/12.
// Copyright (c) 2012 iDevelopSoftware, Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (DumpClassInfo)
- (void)dumpClassInfo;
@end
//
// NSObject+DumpClassInfo.m
// DumpSelectors
//
// Created by Bennett Smith on 1/14/12.
// Copyright (c) 2012 iDevelopSoftware, Inc. All rights reserved.
//
#import "NSObject+DumpClassInfo.h"
#import <objc/runtime.h>
static void dumpClassInfo(Class c, int inheritanceDepth)
{
Class superClass = class_getSuperclass(c);
if (superClass != Nil)
{
dumpClassInfo(superClass, (inheritanceDepth + 1));
}
int i = 0;
unsigned int mc = 0;
const char* className = class_getName(c);
Method* mlist = class_copyMethodList(c, &mc);
for (i = 0; i < mc; i++)
{
Method method = mlist[i];
SEL methodSelector = method_getName(method);
const char* methodName = sel_getName(methodSelector);
const char *typeEncodings = method_getTypeEncoding(method);
char returnType[80];
method_getReturnType(method, returnType, 80);
NSLog(@"%2.2d %s ==> %s (%s)", inheritanceDepth, className, methodName, (typeEncodings == Nil) ? "" : typeEncodings);
int ac = method_getNumberOfArguments(method);
int a = 0;
for (a = 0; a < ac; a++) {
char argumentType[80];
method_getArgumentType(method, a, argumentType, 80);
NSLog(@" Argument no #%d: %s", a, argumentType);
}
}
}
@implementation NSObject (DumpClassInfo)
- (void)dumpClassInfo
{
Class c = object_getClass(self);
dumpClassInfo(c, 0);
}
@end
@BennettSmith
Copy link
Author

The purpose of this class category is to easily print out the names of all selectors for a given Objective-C object. It is implemented as a class category on NSObject. To use this first add NSObject+DumpClassInfo.h and NSObject+DumpClassInfo.m to your project. Then make sure you #import "NSObject+DumpClassInfo.h" in your project. Finally, write a line of code like this:

[self dumpClassInfo];

and it will display a list of all method selectors for this class. Replace self with a different object id to print method selectors for another object.

@barefeettom
Copy link

method_getReturnType provides a single character "@" for objects. Any idea how to obtain the class of the object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment