Objective-C objc_class introduction nsobject
In objective-C, nsobject is the root class of most classes.
@interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY;}
It has an ISA attribute and its type is class.
Apple has made the objc runtime code open source, we download to see what class is http://opensource.apple.com/tarballs/objc4/objc4-493.9.tar.gz
Objc_class
We can see the definition of Class in the <objc. h> file.
typedef struct objc_class *Class;typedef struct objc_object { Class isa;} *id;
Class is a pointer of the objc_class structure type, and ID is a pointer of the objc_object structure type.
The definition of objc_class can be found in <runtime. h>.
struct objc_class { Class isa; Class super_class; const char *name; long version; long info; long instance_size; struct objc_ivar_list *ivars; struct objc_method_list **methodLists; struct objc_cache *cache; struct objc_protocol_list *protocols;} OBJC2_UNAVAILABLE;
Next, let's look at the meaning of each attribute.
Isa
Is an objective-C Class pointer. the instance object has an ISA attribute pointing to class, and the class also has an ISA attribute pointing to meteclass. here is a point. In objective-C, any class definition is an object.
Super_class
Point to the parent class of the class. If the class is already the top-level root class (such as nsobject or nsproxy), super_class is null.
What about their relationship? There's a graph http://www.sealiesoftware.com/blog/class%20diagram.pdf.
Name
Let's take a look at the following code.
id objc_getClass(const char *aClassName){ if (!aClassName) return Nil; // NO unconnected, YES class handler return look_up_class(aClassName, NO, YES);}PRIVATE_EXTERN id look_up_class(const char *aClassName, BOOL includeUnconnected, BOOL includeClassHandler){ BOOL includeClassLoader = YES; // class loader cannot be skipped id result = nil; struct old_class query; query.name = aClassName; retry: if (!result && class_hash) { // Check ordinary classes mutex_lock (&classLock); result = (id)NXHashGet(class_hash, &query); mutex_unlock (&classLock); } if (!result && includeUnconnected && unconnected_class_hash) { // Check not-yet-connected classes mutex_lock(&classLock); result = (id)NXHashGet(unconnected_class_hash, &query); mutex_unlock(&classLock); } if (!result && includeClassLoader && _objc_classLoader) { // Try class loader callback if ((*_objc_classLoader)(aClassName)) { // Re-try lookup without class loader includeClassLoader = NO; goto retry; } } if (!result && includeClassHandler && objc_classHandler) { // Try class handler callback if ((*objc_classHandler)(aClassName)) { // Re-try lookup without class handler or class loader includeClassLoader = NO; includeClassHandler = NO; goto retry; } } return result;}struct old_class { struct old_class *isa; struct old_class *super_class; const char *name; long version; long info; long instance_size; struct old_ivar_list *ivars; struct old_method_list **methodLists; Cache cache; struct old_protocol_list *protocols; // CLS_EXT only const uint8_t *ivar_layout; struct old_class_ext *ext;};
Objc_getclass returns a class from a string, look_up_class. First, create an old_class object, and assign the value of name to this string. If yes, return old_class. after reading the structure of old_class, we can see that the value stored in the name attribute is the name of the class (in fact, name is not the name)
Version
Class version information. The default value is 0.
Info
Some bitwise identifiers used during runtime.
Instance_size
Instance variable size of this class
Ivars
struct objc_ivar_list { int ivar_count; /* variable length structure */ struct objc_ivar ivar_list[1];}
Array of member variables
Methodlists
Method definition array
struct objc_method_list { struct objc_method_list *obsolete; int method_count; /* variable length structure */ struct objc_method method_list[1];}
Objc_cache
Point to the recently used method. Used for method call optimization.
struct objc_cache { unsigned int mask /* total = mask + 1 */; unsigned int occupied; Method buckets[1];};
Protocols
Protocol Array
struct objc_protocol_list { struct objc_protocol_list *next; long count; Protocol *list[1];};