The journey of an OC object

A beginner iOS developer must be curious about how an OC object is created. Today we will take a brief look at it.

  • An object is created and the underlying relationship goes as follows:

For example, FRPerson inherits from NSObject

FRPerson *person =  [[FRPerson alloc] init];
Copy the code
The journey of an object
Person is a local variable (stored in the stack)
The person variable is a pointer to the Person instance object (stored in the heap)
The Person instance object has an ISA pointer to the FRPerson class object (with a global section).
The FRPerson class also has isa Pointers to meta-class objects (with global extents)
The ISA of the FRPerson metaclass object points to the base metaclass object (NSObject metaclass object)(with global extents)
The ISA pointer to the metaclass object of the base class points to itself

Multiple instance objects can be created, and creating one allocates a new memory in the heap via alloc

Class objects and metaclass objects have only one copy in memory, and the same address is obtained many times


Instance object class Object metaclass object

  • Concrete memory structure

  • Instance object

instance
Isa pointer (first 8 bytes)
The value of a member variable
.
  • Class object (class)
class
Isa pointer (first 8 bytes)
Superclass pointer (8 bytes)
Attribute information
Object method information
Protocol information
Member variable information
.
  • Metaclass objects (meta-classes)
meta-class
Isa pointer (first 8 bytes)
Superclass pointer (8 bytes)
Class method information
.
  • Gets instance object class object metaclass object

#import

Person * Person = [[Person alloc] init]; // Class personClass = [person Class]; // Class metaClass = object_getClass(personClass); BOOL isMetaClass = class_isMetaClass(metaClass); NSLog(@"%p,%p,%p,%d",person,personClass,metaClass,isMetaClass); X1000080d8 x100008100 x100494140 / / 0, 0, 0, 1Copy the code

The object_getClass function argument returns the class object if passed in as an instance object, and the metaclass object if passed in as a class object

Class objects and metaclasses are of Class type, that is, they are of the same type but store different information

Class_isMetaClass determines if it is a metaclass object

Superclass pointer

Superclass pointer
The superclass pointer to a class object points to the superclass object of the parent class, all the way to the base class, whose superclass is nil
The superclass of a metaclass object points to the parent metaclass object, all the way to the base metaclass object, butThe superclass of the base metaclass object points to the class object of the base class

Note: According to the final direction of the superclass pointer, if a class calls a class method, it will find the base class object, and the base class object holds the method of the base class object method, if there is an object method with the same name, the system will call this method. (The result is a class calling an object method.)

A corollary to this is that the Runtime runtime does not call methods based on object methods or class methods; Object methods, class methods just determine where they are stored.

A diagram found on the Internet, very image:

  • In the next article, we’ll look at how methods are called.

What is the flow of series -OC method call