From “objc.h”:
typedef struct objc_class *Class;
but in “runtime.h”:
struct objc_class {
Class isa;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
What exactly is Class?
^ That is a forward declaration for an
objc_classpointer. It gives it the nice friendly nameClass.Now lets take a look at the
objc_classstruct:(Removing Objective-C 2.0 checks to shorten it up)
So now we have a struct that points to its own type. But why you ask?
Taken from Inside the Objective-C Runtime, Part 2
So according to that description when you create a class inheriting from
NSObjectyou have anisathat points to its class type which points to its meta class which points to its root class (NSObject) which contains a circular reference to itself. This explains the reason why it requires two steps to determine if an object is an instance or a class.Lets say you create the following class:
If you were able* to traverse the isa pointer you would get the following:
*You can not since the
isais protected. See Cocoa with Love post below for creating your own implementation.Once
c == c->isayou will know you are at the root object. Remember that Objective-C is a proper superset of C, which does not natively have object-oriented constructs such as inheritance. This along with other implementation details, such as pointers to super classes which make up the full class hierarchy, allows Objective-C to provide object-oriented features. For more information on classes and meta-classes see the post on Cocoa with Love: What is a meta-class in Objective-C?