OC is an object-oriented development language, is made up of C, C++, assembly, so what is the nature of the object, in the underlying structure is what, it is worth exploring.

Nature of object

Create a SwwPerson object in main.m and compile it into c++ using the xcrun command.

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main_arm64.cpp
Copy the code

Open main_arm64. CPP file and find SwwPerson. It can be seen that the essence of an object is a structure. The inheritance of an object is the parent class as a member variable 😂

typedef struct objc_object SwwPerson;

struct SwwPerson_IMPL {
	struct NSObject_IMPL NSObject_IVARS;
	NSString *_SwwName;
};
Copy the code

Objc_object isa structure that contains only one member variable isa

struct objc_object {
    Class _Nonnull isa __attribute__((deprecated));
};
Copy the code

The type of ID we commonly use is essentially a pointer to a structure

typedef struct objc_object *id;
Copy the code

The structure type of Class Class is objC_class, which is also a structure pointer

typedef struct objc_class *Class;
Copy the code

isa

A domain

In the program, some information storage does not need a complete byte, only a few bits, to save storage space C language support “bit domain” structure. The following defines a structure and a bit field. The size of the structure is 12 bytes, and the size of the bit field is 4 bytes (an integer multiple of the size of the widest primitive type member). The internal storage of the bit field has its own rules.

Struct str1 {int a; int b; int c; }; Struct str2 {int a:1; int b:2; int c:6; };Copy the code

Union (common body)

Union unions and structs are similar in type definition, variable definition, and usage. However, the members of the union are mutually exclusive and share the same memory address. The sizeof the union is the sizeof the largest element in the union.

union uni
{
    int a;
    int b;
    int c;
}
Copy the code

isa

Isa is structured as isa_t, which isa union bit field. The following is short for isa_t. Bits isa bit field.

union isa_t {
    uintptr_t bits;
    Class cls;
}
Copy the code

Bits in the ARM64 architecture

uintptr_t nonpointer : 1; 0 Pure ISA pointer 1 contains other information Uintptr_t has_ASsoc: 1; Uintptr_t has_cxx_dtor: 1; Uintptr_t shiftcls: 33; uintptr_t shiftcls: 33; Uintptr_t magic: 6; Uintptr_t Weakly_referenced: 1; uintptr_t weakly_referenced: 1 If an object is or has been referred to an ARC weak variable, objects without weak references can be released faster. uintptr_t unused : 1; Uintptr_t has_sidetable_rc: 1; Uintptr_t EXTRA_rc: 19 uses sideTable to store reference countCopy the code

In the debugable source code for ObjC4, use ISA to pull out Pointers to classes.

  1. Shift operation (x86 architecture, ShiftClass 44 bits)

First of all getpersontheisa, because the source code used for debugging, so x86 architecture (not M1), shiftClass 44 bits, 3 bits side, 17 bits left. To leave only ShiftClass, change theisa, 3 to the right, 20 to the left, 17 to the right.

  1. ISA_MASK mask

getpersontheisa, and ISA_MASK to get ShiftClass