Instance Instance object

  1. An Instance object is an object that comes out of the class alloc, and every time you call an alloc, you create a new Instance object
    • The alloc method allocates memory and creates an instance object
    • Init is a custom initialization method that should always be overridden
NSObject *object1 = [[NSObject alloc] init]; NSObject *object2 = [[NSObject alloc] init]; NSObject *p1 = [NSObject alloc]; NSObject alloc = [NSObject alloc]; NSObject *p2 = p1; NSObject *p3 = p1;Copy the code
  1. Object1 and Object2 are instance objects of NSObject
  2. They are two different objects that occupy different memory Spaces
  3. The instance object stores information including :(because isa is also a member variable, the instance object stores only its member variables)
    • Isa pointer
    • Other member variables

Class Class object

NSObject *object1 = [[NSObject alloc] init]; NSObject *object2 = [[NSObject alloc] init]; Class objectClass1 = [object1 Class]; // Class objectClass2 = [object2 Class]; // Class objectClass3 = [NSObject Class]; // The method in the runtime Class objectClass4 = object_getClass(object1); // Class objectClass5 = object_getClass(object2); / / class objectCopy the code
  1. ObjectClass1 – objectClass5 are all class objects of NSObject.
  2. They are the same object. Each class has one and only one class object in memory.
  3. The class object stores the following information in memory:
    • Isa pointer
    • SuperClass pointer
    • Of the classattributeInformation (@property)
    • Of the classObject methodsInformation (instance Method)
    • Of the classagreementInformation (Protocol)
    • Of the classMember variables(ivar)
    • · · · · · ·

Meta-class metaclass object

Class metaClass = object_getClass([NSObject class]);

  1. MetaClass is a meta-class object of NSObject.
  2. Each class has one and only one meta-class object in memory
  3. Meta-class objects and class objects have the same structure in memory, but their purpose is different. The main information stored in memory is:
    • Isa pointer
    • Superclass pointer
    • Of the classClass methodInformation (class Method)

Methods involved

  1. Class metaClass = object_getClass([NSObject class]);The obJ passed in May be an instance object, a class object, or a meta-class object
// Pass in an instance object -> return a class object -> return a meta-class object -> return a meta-class object NSObject Meta-class object class object_getClass(id obj) {if (obj) return obj->getIsa(); else return Nil; }Copy the code
  1. Class objc_getClass(const char *aClassName)

    • Pass in the string class name
    • Returns the corresponding class object
  2. – (Class) Class; : Returns the class object

 (Class) {
     return self->isa;
 }
 
 (Class) {
     return self;
 }

Copy the code