Every time I go back into understanding Objective-C runtime, I end up messing my brain. As far as I understand (from reading some awesome write-ups like Metaclasses, Obj-C Runtime) Objective-C classes are actually class objects e.g. NSString is itself an object to its Root Meta Class. Moreover its so called ivar "isa" pointer keeps all the basic details like super class, method lists, name etc. Below are some questions based on this theory only -
(1). Check the current declaration of objc/runtime.h and you'll find the following struct declaration:
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
> #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;
I hope this means that from Objective-C 2.0 all there is available in this struct is "isa" pointer. This seems to fade all the logic from this runtime theory. Where does these details like super class, method list etc. stored now?
(2). Technically isa is pointer to struct objc_class which itself has isa as its member. So when I try accessing (in AppDelegate for say):
NSLog(@"class isa ptr %@", isa->isa);
or
NSLog(@"class isa %@", ((Class)[NSString class])->name);
I receive a compile-time error:
- Member reference base type 'Class' is not a structure or union.
Why? I think its the correct syntax to access structures members
(3). Printing 'Class' (I'm referring to typedef struct objc_class *Class
) object on console using NSLog displays Objective-C class' name.
NSLog(@"NSObject class %@", [NSObject class]);
How is that so? class is a class method that returns 'Class' object. If class name was to be printed, it should be printed like this:
NSLog(@"NSObject class %@", ((Class)[NSObject class])->name);
Help me figure out what is there that I am missing.