This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
1. Optimization of WWDC class structure
Clean Memory and dirty Memory are mentioned in the WWDC Runtime optimization for classes
1.1 Clean the Memory
- Memory that does not change after loading
class_ro_t
It’s read-only. It belongs toClean Memory
- Can be removed to save more memory. The system can be reloaded from disk if you need it.
1.2 Dirty Memory
- Memory that changes while the process is running
- A class structure is once used
Dirty Memory
Because the runtime writes new data, such as the method cache Dirty Memory
Is why class data is divided into two parts
Dirty Memory is more expensive than Clean Memory because it needs to be retained throughout the process. Most of the class data is stored in Clean Memory by separating out the data that never changes. The class structure becomes Dirty Memory once used because the runtime writes new data to it, such as creating a new method cache and pointing to it from the class. So class_rw_t comes in, it reads and writes the inheritance of the class, it keeps track of the methods, properties, protocols, etc., but only about 10% of the class will change its methods, so class_rw_ext_t comes in, 90% of the class will not need class_rw_ext_t, This saves half the space of class_rw_t. The following figure
2. Properties and member variables
Before iOS5, you defined a member variable in curly braces, and you did the at sign property declaration, and you did the at sign synthesize method in at sign implementation. The reason for this is that Apple converted the default compiler from GCC to LLVM (low level Virtual Machine), so you no longer need to declare instance variables for properties. The normal way to write a property before you change it requires the member variable +@property+@synthesize member to facilitate those three steps. With LLVM, when the compiler finds no new instance variables during compilation, it generates an instance variable starting with an underscore. So now departments no longer have to declare an instance variable. Now the @property declaration not only generates a member variable of type _, but also generates a setter/getter method.
- Properties = underlined member variable +setter+getter method.
- Instance variables: Special member variables (instantiations of classes).
2.1 Differences between member variables and attributes
- Member variables: At the bottom level are just declarations of variables
- Properties: The system automatically adds the _ property name variable at the bottom level and generates both the setter and getter methods
2.2 Differences between member variables and instance variables
- An instance variable is a special kind of member variable
- Member variables are basic data types
- Instance variables are object types, for example:
NSObject
type NSString
Is a constant type and belongs to a member variable
3.isKindOfClass
3.1 Functions of methods
Whether you call the isKindOfClass method on a class object or an instance object, the entry function is objc_opt_isKindOfClass find objc_opt_isKindOfClass
BOOL objc_opt_isKindOfClass(id obj, Class otherClass) { #if __OBJC2__ if (slowpath(! obj)) return NO; Class cls = obj->getIsa(); if (fastpath(! cls->hasCustomCore())) { for (Class tcls = cls; tcls; tcls = tcls->getSuperclass()) { if (tcls == otherClass) return YES; } return NO; } #endif return ((BOOL(*)(id, SEL, Class))objc_msgSend)(obj, @selector(isKindOfClass:), otherClass); }Copy the code
- Gets the Class to which object ISA points and compares it to the Class passed in
- Iterating over the object ISA points to the parent Class of the Class, compared to the Class passed in
The isKindOfClass method does:
- Class object
- The Class of metaclass VS
- Traversal: Class superclass VS Class
- Instance objects
- Class VS Class
- Traversal: Class superclass VS Class
3.2 Validate class objects and instance objects
Example: Encapsulate the isKindOfClass function
void isKindOfClassDemo(){ BOOL re1 = [(id)[NSObject class] isKindOfClass:[NSObject class]]; BOOL re2 = [(id)[LGPerson class] isKindOfClass:[LGPerson class]]; NSLog(@"NSObject class object: % HHD ", re1); NSLog(@"LGPerson class object: % HHD ", re2); BOOL re3 = [(id)[NSObject alloc] isKindOfClass:[NSObject class]]; BOOL re4 = [(id)[LGPerson alloc] isKindOfClass:[LGPerson class]]; NSLog(@"NSObject instance object: % HHD ", re3); NSLog(@"LGPerson instance object: % HHD ", re4); }Copy the code
Call the isKindOfClassDemo function
isKindOfClassDemo(); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- NSObject class objects: 1 LGPerson class objects: 0 NSObject instance objects: 1 LGPerson instance objects: 1Copy the code
3.3 analysis
- Res1 is passed in
NSObject
Class to get the metaclass withNSObject
No, continue to look for the parent class of the metaclassNSObject
Equal to the value passed in, returns true.
-
- Res2 is passed in
LGPerson
Class to get the metaclass withLGPerson
No, continue to look for the parent class of the metaclassNSObject
The metaclass of, which is still different from the value passed in, keeps going upNSObject
The superclass of the metaclass isNSObject
Still not. It’s just up therenil
And return false
- Res2 is passed in
- Res3 is passed in
NSObject
Instance of, get the object’s class, andNSObject
If yes, return true - Res4 is passed in
LGPerson
Instance of, get the object’s class, andLGPerson
If yes, return true
4 isMemberOfClass
4.1 Function of the method
Find the isMemberOfClass method
+ (BOOL)isMemberOfClass:(Class)cls {
return self->ISA() == cls;
}
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
Copy the code
- Class methods: Get the metaclass of the class and compare it to the CLS passed in
- Instance method: gets the class to which the object belongs and compares it with the CLS passed in
4.2 Verifying class objects and instance objects
Encapsulates the isMemberOfClass method
void isMemberOfClassDemo(){ BOOL re1 = [(id)[NSObject class] isMemberOfClass:[NSObject class]]; BOOL re2 = [(id)[LGPerson class] isMemberOfClass:[LGPerson class]]; NSLog(@"NSObject class object: % HHD ", re1); NSLog(@"LGPerson class object: % HHD ", re2); BOOL re3 = [(id)[NSObject alloc] isMemberOfClass:[NSObject class]]; BOOL re4 = [(id)[LGPerson alloc] isMemberOfClass:[LGPerson class]]; NSLog(@"NSObject instance object: % HHD ", re3); NSLog(@"LGPerson instance object: % HHD ", re4); }Copy the code
Call the isMemberOfClassDemo function
isMemberOfClassDemo(); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- NSObject class objects: 0 LGPerson class objects: 0 NSObject instance objects: 1 LGPerson instance objects: 1Copy the code
4.3 analysis
- Res1 is
NSObject
Class calls class methodsisMemberOfClass
withNSObject
Class comparison, obviously,NSObject metaclass
withNSObject
Are not equal to each other, so return false - Res2 is
LGPerson
Class calls class methodsisMemberOfClass
withLGPerson class
The comparison,LGPerson metaclass
withLGPerson
Are not equal to each other, so return false - Res3 is
NSObject
Calls instance methods for an instance ofisMemberOfClass
withNSObject
Class comparison, they are obviously the same, so return true - Res4 is
LGPerson
Calls instance methods for an instance ofisMemberOfClass
withLGPerson
Class comparison, obviously they are the same, so returntrue