This article will record the objective-C instance object, class object, metaclass object related information

InstanceObject (InstanceObject)

An instance object is an object instantiated by a class and represents something concrete. In OC, it is as follows:

Person *man = [Person new];
Copy the code

Person is the class, and man is the object instantiated by Person, representing a concrete thing, namely man.

Instance objects are created by alloc or new operations on class objects, which copy the member variables of the class to which the instance belongs, but not the methods defined by the class. When an instance method is called, the system looks for the method to which the corresponding selector of the message points from the class’s method list and its parent class’s method list based on the instance’s ISA pointer. Let’s take a look at the definition:

/// Represents an instance of a class.
struct objc_object {

    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;

};
Copy the code

This structure has only one ISA variable that points to the class to which the instance object belongs. Any structure that starts with a pointer and points to a class structure can be considered objC_Object, and the most important feature of an object is that it can send messages to it.

Class object (Class)

Class objects are defined by the programmer and created at run time by the compiler. They have no instance variables of their own. Note that the list of member variables and instance methods of the class belong to the instance object, but are stored in the class object. Let’s look at the Class definition:

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

typedef struct objc_class *Class;
Copy the code

You can see that the Class is represented by the Class type, which is a pointer to the objC_class structure type. Let’s look at the definition of the objc_class structure:

struct objc_class { Class isa; // Pointer to the owning Class (_Nonnull) Class super_class; // const char *name; // Class name (_Nonnull) long version; // Class version information (default: 0) long info; // Class information (some bit identifier for run-time use) long instance_size; Struct objc_ivar_list *ivars; Struct objc_method_list * *methodLists; Struct objc_cache *cache; Struct objc_protocol_list *protocols; // protocol linked list};Copy the code
  • Isa pointer isa pointer to the objc_class structure of the same type as Class. A pointer to a Class object points to its own Class, that is, a metaclass. The metaclass stores the class method of the class object. When accessing the class method of a class, the ISA pointer will be used to find the function pointer corresponding to the method from the metaclass

  • Super_class is the superclass object that this class inherits, or NULL if the class is already the topmost root class (such as NSObject or NSProxy)

  • Ivars is a pointer to objc_ivar_list to store the address of each instance variable

  • Info is a bit identifier used at runtime, such as:

    CLS_CLASS (0x1L) indicates that the class is a normal class, and CLS_META (0x2L) indicates that the class is a metaclass

  • MethodLists are used to store a list of methods. Based on the identity information in info, when the class is a normal class, the stored methods are instance methods. Class method stored if metaclass

  • Cache is used to cache recently used methods. When calling a method, the system looks it up in the cache first, and then iterates through methodLists to get the desired method if there is no lookup

Metaclass

A metaclass isa class of class objects, and each class has its own metaclass, which is the class to which the isa pointer in the objc_class structure points. Objective-c class methods are the root reason for using metaclasses, because they store the methods called by the corresponding class object.

So as you can see from the figure above, when sending messages to instance objects or class objects, the rules for finding the method list are as follows:

  • When you send a message to an instance object, the message is a list of methods of the class that is looking for the object (instance methods)
  • When a message is sent to a class object, the message is looking for a list of methods on the metaclass of that class (class methods)

A metaclass, like the class before it, is an object that can also call its methods. So that means it has to have a class as well. All metaclasses use the root metaclass as their class. For example, all subclasses of NSObject have metaclasses that use NSObject as their classes.

According to this rule, all metaclasses use the root metaclass as their class, and the metaclass of the root metaclass is itself. That is, the isa pointer to the metaclass of the base class points to itself.

To better understand the relationship between objects, class objects, and metaclasses, we can refer to the following diagram:

To summarize the isa reference and inheritance relationships between instance objects, class objects, and metaclass objects:

  • The ISA of the instance object points to the class, and the ISA of the class points to the metaClass.
  • The superClass of a class refers to its parent, or nil if the class is the root class
  • The ISA of a metaclass points to the root metaclass, or itself if the metaclass is the root metaclass
  • The superClass of a metaclass points to the parent metaclass and, in the case of the root metaclass, to the root class

reference

Instances of OC underlying principles, class objects, metaclass objects

Learn about iOS classes and objects