This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Today the sun is just, the breeze is not dry ~
What a day for talking ~
Today we’re going to chatter about objects in iOS
Speaking of objects, there are several types of objects in iOS
Three types of objects in iOS
- Instance object
- Class object (class)
- Metaclass objects (meta-classes)
For example, at this point we have two classic two classes,Persion inherits from NSObject and Student inherits from Persion
@interface Persion : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) int age; @end @interface Student: Persion @property (nonatomic, copy) NSString *teacher; @endCopy the code
Instance objects
Instance objects are the classes we generate by alloc init, for example:
Student *s = [[Student alloc] init]; // Instance objectCopy the code
So what do instance objects store in memory?
isa
Pointer to the- Member variables
The isa pointer points to the class object of the instance object, and the member variables are the property variables of the instance object
In the current example, the isa pointer points to the Student class and the member variable is just teacher
Class object
Class objects are what we call classes and can be obtained using the class method, or object_getClass().
Class objects mainly store:
isa
Pointer to thesuperClass
Pointer to the- Class attribute information (@Property) Object method information of the class
- Class protocol information, class member variables information
Among them:
isa
Pointers to metaclass objectssuperClass
Pointer to the superclass object, hereStudent
The superclass of theta is thetaPersion
- Class attribute information refers to the attribute information of the instance object, here refers to
teacher
The information of - The object method information of a class is instance method information
- The protocol information of a class is the protocol information of the class
- The member variable information of a class is the member variable of an object
Yuan class object
Each class object has a metaclass object corresponding to it in memory, which can be obtained via object_getClass()
Note that the metaclass object is obtained via object_getClass () and the parameter is passed as a class object
So what’s in a metaclass object:
isa
Pointer to thesuperClass
Pointer to the- Class method information for a class
Among them:
- The ISA pointer points to the root metaclass, which in this case points to nsobject’s metaclass
- The superClass pointer points to the superClass’s metaclass, which in this case points to Persion’s metaclass
Here’s a classic picture:
The SubClass is Student, the SuperClass is Persion, and the Root class is NSObject
That’s the introduction to objects in iOS