According to the apple official website to do some verification of the diagram
1. Check the number of the same class in memory
Class class1 = [HTPerson class];
Class class2 = [HTPerson alloc].class;
Class class3 = object_getClass([HTPerson alloc]);
Class class4 = [HTPerson alloc].class;
NSLog(@"\n%p-\n%p-\n%p-\n%p",class1,class2,class3,class4);
Copy the code
The following output is displayed. To prove that the same class is in memory, only one address will be created.
0x100004990-
0x100004990-
0x100004990-
0x100004990
Copy the code
2. Query the ISA address of the class
The memory address of the class is obtained by system method, and its internal structure is output by LLDB
NSObject *object1 = [NSObject alloc]; // NSObject Class Class = object_getClass(object1); // NSObject metaClass = object_getClass(Class); // NSObject rootMetaClass Class rootMetaClass = object_getClass(metaClass); // NSObject rootMetaClass Class rootRootMetaClass = object_getClass(rootMetaClass); NSLog (@ "\ n \ n % p % p instance objects class p metaClass \ n \ n % % p root metaClass \ n % p spikes metaClass", object1, class, metaClass, rootMetaClass, rootRootMetaClass);Copy the code
The following output is displayed:
Conclusion:
- Isa –> class of instance object,
- Class isa –> metaclass,
- Metaclass object ISA –> root metaclass
- Root metaclass ISA –> root metaclass
3. Class inheritance
According to the inheritance relation in the figure (class-parent class-parent class-NULL), the following verification is done:
Class nSuperClass = class_getSuperclass(NSObject.class); //NSObject metaclass parent NSLog(@" htteacher-class address %p", htteacher.class); NSLog(@"HTPerson- parent address %p", htperson.class); NSLog(@" nsobjject - parent class address %p", nsobject.class); NSLog(@"NSObjectSuper- parent %@ -- parent %p",nSuperClass,nSuperClass);Copy the code
2021-06-20 18:34:30.592061+0800 002-ISA Analysis [16955:397181] HTTeacher- Class Address 0x100008b40 2021-06-20 18:34:30.592425+0800 [16955:397181] HTPerson- Parent address 0x100008AF0 2021-06-20 18:34:30.592493+0800 002-ISA Analysis [16955:397181] NSObject- Parent class address 0x7FFF94a3F118 2021-06-20 18:34:30.592555+0800 002- ISA analysis [16955:397181] NSObjectSuper- Parent class (NULL) -- Parent class 0x0Copy the code
Show that HTPerson inherits from NSObject, and NSObject inherits from nil
4. The internal structure of the class
Check the source code as follows
Typef struct objc_class *Class Class is a pointer type to objc_class.
- Search objc_class to find the following three members:
struct objc_class : objc_object { ... // Class ISA; // Point to metaclass 8 bytes Class superclass; // Parent class 8 bytes cache_t cache; // Cache structure 16 bytes class_data_bits_t bits; // Object methods and properties... }Copy the code
- To view
class_data_bits_t
struct class_data_bits_t { ... class_rw_t* data() const { return (class_rw_t *)(bits & FAST_DATA_MASK); }... }Copy the code
- To view
class_rw_t
There are several methods in it.
Struct class_rw_t {// list of methods const method_array_t methods() const {... } // Property list const property_array_t properties() const {... } // Proxy method const protocol_array_t protocols() const {... }}Copy the code
5. Verify the storage information based on the memory addressMemory address translation is used
)
@interface LGPerson : NSObject{
NSString *subject;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *hobby;
- (void)sayNB;
+ (void)say666;
Copy the code
Get the following output 🙁x/4gx
Print the memory of the class.p/x
Print address)
-
Get property list
Obtaining the bits address requires byte translation, which is 32 bits.
-
Get instance method (similar to get property list
properties
To — — >methods()
)
-
Gets a list of members of the class
-
Get a list of class methods (to get from a metaclass)
Gets the address of the metaclass
6. Summary
Classes include ISA, superclass, cache, and bits member variables. Bits stores attribute list, instance method list, member variable list, protocol list, and so on. Where the class methods of the class are stored in bits of the metaclass.
When you’re done, go out and exercise.