Please indicate the source for reprint http://blog.csdn.net/u014205968/article/details/64921601
Each object in object-oriented must rely on a class to create, so the isa
pointer of the object points to the class to which the object belongs. According to this class template, instance variables, instance methods, etc. can be created.
For example, the following code
NSString* str = @"Hello World";
From the above, we know that this str
object is essentially a objc_object
structure, and the member variable isa
pointer of this structure indicates that str is a NSString
, so this isa
points to a NSString
class. This NSString
class is actually a class object. If you don't understand, continue to read it.
class object/metaclass
objc_class
Continue to view the definition of the structure
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; /* Use `Class` instead of `struct objc_class *` */
struct objc_classs
The data stored in the structure is called 元数据(metadata)
. Through the name of the member variable, we can guess that it stores the pointer to the parent class, the name of the class, the version, the instance size, the list of instance variables, the list of methods, the cache, the list of protocols to be followed, etc., This information is enough to create an instance. The first member variable of the structure is also a isa
pointer, which means that it Class
is actually an object. We call it a singleton, which is generated at compile time to create an instance object 类对象
. 类对象
Therefore, the chestnut in the previous article should actually be expressed as what does str的isa指针指向了NSString类对象
the pointer of this structure isa
point to?
类对象
The 元数据
information stored in is how to create an instance, so 类对象
and 类方法
where should it be created? It is isa
created from the structure pointed to by the pointer, and 类对象
the isa
pointer pointed to by us calls it 元类(metaclass)
, and all the information needed 元类
for creation 类对象
and creation is stored in it 类方法
, so the whole structure should be as shown in the following figure: