Objective-C
Objective-c is a superset of C language, we usually write OC, the underlying implementation is C\C++ code, OC objects, classes are based on C\C++ structure implementation. You can convert OC code to C\C++ code using the following command (for reference only, not necessarily 100% accurate)
Xcrun-sdk iphoneos clang-arch arm64-rewrite-objc <OC source file > -o < output.cpp file >Copy the code
If you need to link to other frameworks, use the -framework argument, such as -framework UIKit
NSObject
In general, objects we write inherit from NSObject, so figuring out NSObject is pretty critical
- NSObject underlying implementation of relevant source code
Struct NSObject_IMPL{Class *isa} //Class isa pointer to the objc_class structureCopy the code
NSObject has only Class ISA Pointers, so the address of obj is the address of the ISA pointer. It appears to take up only 8 bytes (8 bytes for pointer variables), but read the OC source code to see that 16 bytes are allocated. You can use the following API to view memory usage
Class_getInstanceSize ([NSObject class]); // How many bytes an instance object actually occupies malloc_size((__brige const void *)obj);Copy the code
A structure of a class that inherits NSObject
// for example @interface Person: NSObject @property (nonatomic, assign) int age; @end struct Person_IMPL{ stuct NSObject_IMPL NSObject_IVARS; int _age; } struct Person_IMPL{Class *isa; struct Person_IMPL{Class *isa; int _age; }Copy the code
OC Object Classification
OC objects fall into three categories: instance objects, class objects, and meta-class objects.
- Instance object: An object derived from the class alloc. Each alloc creates a new instance object
NSObject *obj1 = [[NSObject alloc] init]; NSObject *obj2 = [[NSObject alloc] init]; // Obj1 and obj2 are both instance objects. They are two different objects occupying different memoryCopy the code
Instance objects store information in memory such as isa Pointers and other member variables.
- Class object (Class object)
NSObject *object1 = [[NSObject alloc] init];
NSObject *object2 = [[NSObject alloc] init];
Class objectClass1 = [object1 class];
Class objectClass2 = [object2 class];
Class objectClass3 = object_getClass(object1);
Class objectClass4 = object_getClass(object2);
Class objectClass5 = [NSObject class];
Copy the code
ObjectClass1 ~ objectClass5 are all class objects of NSObject. They are all the same object. Each class has one and only one class object in memory. Isa pointer Superclass pointer class attribute information (@property), class object method information (instance method) class protocol information (protocol), class member variable information (IVAR)
- Meta-class (metaclass object)
Class objMetaClass = object_getClass([NSObject Class]); Class objMetaClass = [[NSObject Class] Class]; * /Copy the code
ObjectMetaClass is a meta-class object of NSObject. Each class has one and only one meta-class object in memory. A meta-class object has the same memory structure as a class object, but its purpose is different. Information stored in memory mainly includes: isa pointer superClass pointer class method information……
- The structure of the Class
//struct objc_class struct objc_class{Class isa; Class superclass; Cache_t cache; // Method cache class_data_bit_t bit; Struct class_rw_t {// uint32_t flags; uint32_t version; const class_ro_t *ro; method_array_t methods; //method_t array property_array_t properties; Protocol_array_t protocols; Class firstSubclass; Class nextSiblingClass; char *demangledName; // uint32_t flags; // uint32_t flags; uint32_t instanceStart; uint32_t instanceSize; #ifdef __LP64__ uint32_t reserved; #endif const uint8_t * ivarLayout; const char * name; // Class name method_list_t * baseMethodList; // List of methods protocol_list_t * baseProtocols; // protocol list const ivar_list_t * ivars; // List of member variables const uint8_t * weakIvarLayout; property_list_t *baseProperties; }Copy the code
Isa pointer and Supclass pointer
- The ISA pointer of instance points to CLAS. When instance calls an object method, it finds the class through the ISA pointer and finds the corresponding object method to call
- The isa pointer to the class points to the meta-class. When invoking a class method, the isa of the class finds the meta-class and the corresponding class method
Starting with Arm64, isa Pointers are optimized so that isa & ISA_MASK is the real address value
- When instance calls a method of a superclass object, it first finds the class through the ISA pointer, then finds the superclass through the superclass pointer, and then finds the corresponding object method
- A superclass pointer to a meta-class object points to a meta-class object of its superclass
When calling a superclass method, find the meta-class through the isa of the class, find the superclass through the superclass pointer of the meta-class object, and then find the corresponding class method to call