The nature of OC objects (top) : The underlying implementation principle of OC objects

The nature of the OC object (part 2) : the ISA&superclass pointer

Objective-c objects, OC objects for short, fall into three main categories

  • instanceObject (Instance object)
  • classObject (Class object)
  • meta-classObject (metaclass object)

Instance Instance object

An instance object is an object created using the alloc method, and each call to the alloc method generates a new instance object

NSObjcet *object1 = [[NSObject alloc] init];
NSObjcet *object2 = [[NSObject alloc] init];
Copy the code

Object1 object2 above are both instance objects of NSObject, and because they have their own memory, they are two different objects

The instance object holds information in memory including

  • isaPointers (because basically all our common classes and custom classes inherit fromNSObjectSo what we’re talking about hereinstanceThey all containisaPointer)
  • otherMember variables

Class class object

The class object is used to describe an instance object. It contains the property information (@property), instance method information (object method information), protocol information (protocol information), and ivAR information of a class. There are also two Pointers to the class object, isa and superclass.

You can get the class object of a class by using the following method

#import <Foundation/Foundation.h> #import <objc/runtime.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!" ); NSObject *obj1 = [[NSObject alloc] init]; NSObject *obj2 = [[NSObject alloc] init]; Class objClass1 = [NSObject class]; Class objClass2 = [obj1 class]; Class objClass3 = [obj2 class]; Class objClass4 = object_getClass(obj1); Class objClass5 = object_getClass(obj2); NSLog(@"\nobjClass1:%p\nobjClass2:%p\nobjClass3:%p\nobjClass4:%p\nobjClass5:%p", objClass1, objClass2, objClass3, objClass4, objClass5); } return 0; }Copy the code

As you can see from the print above, the class object of a class is unique and only one copy is stored in memory, which makes sense because only one copy of the information in the class object is enough.

Meta-class metaclass object

The meta-class object is used to describe a class object. Like classes, metaclass objects have only one copy in memory. It stores isa pointer + Superclass pointer + class method information (+ method).

Here are some apis for metaclass objects:

  • Object_getClass (< class object >);Gets a metaclass object as a class object
  • Class_isMetaClass (<class object /meta-class object >);Check whether it is a metaclass object

Since both class and meta-class objects are available via object_getClass, they are of the same type, typedef struct objc_class * class. Each of them uses some member variables of Class to implement its own functionality.

The difference between several common methods
(a)Class objc_getClass(const char *aClassName)
  • The input parameter is a string, which is the class name
  • The return value is the correspondingClass object
  • Because we can only define the name of the class by string, this method can only returnClass object
(b)Class object_getClass(id obj)
  • The ginsengobjCan beThe instance objects,Class objectorMeta - class object
  • The return value
    • The incomingThe instance objects, returns the correspondingClass object
    • The incomingClass object, returns the correspondingMeta - class object
    • The incomingMeta - class objectTo return toNSObject (base class)theMeta - class object
/***********************************************************************
* object_getClass.
* Locking: None. If you add locking, tell gdb (rdar://7516456).
**********************************************************************/
Class object_getClass(id obj)
{
    if (obj) return obj->getIsa();
    else return Nil;
}
Copy the code

As we can see from the object_getClass source implementation above, this method returns an ISA pointer.

I used my art skills to describe the relationship between various IsAs by hand, so that the isa of instance can obtain its class object, and the ISA pointer of class can obtain its meta-class object. As for the isa pointer of meta-class, We’ll leave that for the next chapter.

(c)- (Class)class & + (Class)class

A class has this method in both instance methods and class methods. Let’s test that out in code

#import <Foundation/Foundation.h> #import <objc/runtime.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSObject *obj1 = [[NSObject alloc] init]; Class objClass1 = [NSObject class]; Class objClass2 = [obj1 class]; Class objClass3 = [objClass1 class]; Class objClass4 = [objClass2 class]; // There is only one NSLog(@"\nobjClass1:%p\nobjClass2:%p\nobjClass3:%p\nobjClass4:%p \ nobjp ", objClass1, objClass2, objClass3, objClass4); } return 0; }Copy the code

The output is as follows

Based on the results, we can conclude that a class object calls the class method and returns not its meta-class object, but itself. In short, either – (Class) Class or + (Class) Class can only return a Class object of one Class


The nature of OC objects (top) : The underlying implementation principle of OC objects

The nature of the OC object (part 2) : the ISA&superclass pointer

Special note

This series of articles are summarized from the basic principles of OC taught by MJ in Tencent class. The relevant pictures are taken from the courseware in the course.