Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.


/// An opaque type that represents an Objective-C class.

typedef struct objc_class *Class;
Copy the code
struct objc_class { Class _Nonnull isa OBJC_ISA_AVAILABILITY; #if ! __OBJC2__ Class _Nullable super_class OBJC2_UNAVAILABLE; const char * _Nonnull name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE; struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE; struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE; struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; #endif } OBJC2_UNAVAILABLE; /* Use `Class` instead of `struct objc_class *` */Copy the code

We found the OC2.0 structure obsolete. C++ structures are basically the same as classes, right

struct objc_class : objc_object { // Class ISA; Class superclass; cache_t cache; // formerly cache pointer and vtable class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags class_rw_t *data() { return bits.data(); } bool isMetaClass() { assert(this); assert(isRealized()); return data()->ro->flags & RO_META; } // NOT identical to this->ISA when this is a metaclass Class getMeta() { if (isMetaClass()) return (Class)this; else return this->ISA(); } bool isRootClass() { return superclass == nil; } bool isRootMetaclass() { return ISA() == (Class)this; } size_t instanceSize(size_t extraBytes) { size_t size = alignedInstanceSize() + extraBytes; // CF requires all objects be at least 16 bytes. if (size < 16) size = 16; return size; }};Copy the code
struct objc_object {

private:

    isa_t isa;

public:

    // ISA() assumes this is NOT a tagged pointer object

    Class ISA();

    // getIsa() allows this to be a tagged pointer object

    Class getIsa();

    bool isClass();


    // object may have associated objects?

    id retain();

    void release();

    id autorelease();

    // Implementations of retain/release methods

    id rootRetain();

    bool rootRelease();

    id rootAutorelease();

    bool rootTryRetain();

    bool rootReleaseShouldDealloc();

    uintptr_t rootRetainCount();

    void rootDealloc();

};
Copy the code

Combine the two:

struct objc_class { Class ISA; Class superclass; cache_t cache; // formerly cache pointer and vtable class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags class_rw_t *data() { return bits.data(); }};Copy the code
struct class_rw_t {
    // Be warned that Symbolication knows the layout of this structure.

    uint32_t flags;

    uint32_t version;

    const class_ro_t *ro;

    method_array_t methods;

    property_array_t properties;

    protocol_array_t protocols;

    Class firstSubclass;

    Class nextSiblingClass;

    char *demangledName;

#if SUPPORT_INDEXED_ISA

    uint32_t index;

#endif
};
Copy the code
struct class_ro_t { 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; protocol_list_t * baseProtocols; const ivar_list_t * ivars; // Uint8_t * weakIvarLayout; property_list_t *baseProperties; method_list_t *baseMethods() const { return baseMethodList; }};Copy the code

Struct objc_class struct objc_class

struct objc_class{ Class isa; \ Class superclass; \ cache_t cache; // Method cache class_data_bits_t bits; // To get specific class information};Copy the code

bits & FAST_DATA_MASK

Get specific class information.

struct class_rw_t{ uint32_t flags; \ uint32_t version; \ const class_ro_t *ro; // method list \ property_list_t *properties; Const protocol_list_t *protocols; // Protocol list Class firstSubclass; The Class nextSiblingClass; char *demangleName; The \};Copy the code
struct class_ro_t { 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; protocol_list_t * baseProtocols; const ivar_list_t * ivars; // List of member variables const uint8_t * weakIvarLayout; property_list_t *baseProperties; };Copy the code

Where does the isa pointer to the object point?

The ISA of an instance object points to a class object

The ISA of a class object points to a meta-class object

The ISA pointer to a meta-class object points to a meta-class object of the base class.

Where is the OC class information stored?

Object methods, properties, member variables, and protocol information are stored in class

In the object

Class method, stored in a meta-class object