Question:

  1. Where does the isa pointer to the object point
  • The ISA of the instance object points to the class object
  • The ISA of a class object points to a metaclass object
  • The ISA of a metaclass object points to a metaclass object of the base class
  1. Where is information stored for oc objects?
  • Object methods, properties, member variables, and protocols are stored in class objects
  • Class methods are stored in metaclass objects
  • The value of a member variable is stored in the instance object

Objects in object-c are divided into three main types:

  • Instance object (instance object)
  • Class object (Class object)
  • Meta-class objects (metaclass objects)

The instance objects

  • An instance object is an object that comes out of the alloc class, and every time you call that alloc, you create a new instance object
  • Memory stores:
  • Isa pointer
  • Other member variables

Class object

  • Each class object has one and only one class object in memory
  • Memory stores:

  • Isa pointer
  • Superclass pointer
  • Class properties (@property), class object methods (instance > * method)
  • Class protocol information (Protocol), class member variable information (IVAR)
  • Other member variables

Meta – class object

  • That is, the objects to which the ISA of a class object points. Each class has one and only one meta-class object in memory
  • Meta-class objects have the same memory structure as class objects, but their purpose is different
  • throughobject_getclassGet the metaclass object
  • Memory stores:

  • Isa pointer
  • Superclass pointer
  • Class properties (@property), class object methods (instance > * method)
  • Class protocol information (Protocol), class member variable information (IVAR)
  • Other member variables

The relationship between isa and superclass Pointers to three types of objects

  • Isa pointer

  • Superclass pointer to the class object

  • Superclass pointer to a meta-class object

  • conclusion

The class structure

At the bottom, both class and meta-class are objC_class structures

struct objc_class { Class ISA; Class superclass; cache_t cache; // Method cache class_data_bits_t bits; FAST_DATA_MASK = &fast_data_mask = &fast_data_mask };Copy the code

Class_data_bits_t

struct class_data_bits_t { class_rw_t* data; // Store class information... }Copy the code

Class_rw_t:

struct class_rw_t { uint32_t flags; uint16_t witness; explicit_atomic<uintptr_t> ro_or_rw_ext; Class firstSubclass; Class nextSiblingClass; using ro_or_rw_ext_t = objc::PointerUnion<const class_ro_t *, class_rw_ext_t *>; const ro_or_rw_ext_t get_ro_or_rwe(); const class_ro_t *ro(); const method_array_t methods(); // Method list const property_array_t properties(); Const protocol_array_t protocols(); // Protocol list... };Copy the code

Where ro structure is:

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; const uint8_t * weakIvarLayout; property_list_t *baseProperties; . };Copy the code

Small details: Define ISA_MASK 0x0000000ffffffff8ULL Identifies the address of the class or metaclass object that ISA points to, with the last three digits being zeros.