10

In the NSObject protocol, it defines a method that is similar to this:

-(Class) class

What type of object is the Class object? Or is it even an object? What can I do with the object? Can I get the base class or adopted protocols?

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201

2 Answers2

16

Class is itself a class defined by the Objective-C runtime, akin to the Class class in Java. For example, you can use the function class_getClassName() to get the name of a class:

NSObject *o = [[[NSObject alloc] init] autorelease];
NSLog(@"%s\n", class_getClassName([o class]));  // prints "NSObject"

You can do all kinds of introspection/reflection with Class objects; see the Objective-C runtime reference for details.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
4

It is now

NSObject *o = [[NSObject alloc]init];
NSLog(@"%s\n", object_getClassName([o class]));

object_getClassName instead of class_getClassName