Runtime
What is Runtime?
Runtime is a set of C and C++, assembler framework that provides runtime functionality for OC.Copy the code
OC object
The OC object becomes a struct method list property list protocol list when compiledCopy the code
Methods -> How are messages sent
The method generates a function id with the argument (id self,SEL _cmd) at compile time for the message receiver _cmd for the method number -> find the directory where the method address is similar to a book, pointing to a pointer to the specific implementation of the functionCopy the code
SEL-IMP
Objc_msgSend CacheLoopup CacheHit Find cache CheckMiss add AddCopy the code
Method to find the
SuperClass = > superClass = > superClass = > superClass = > superClass = > superClass = > superClassCopy the code
Dynamic method parsing
+(BOOL)resolveInstanceMethod:(SEL)sel {
return [super resolveInstanceMethod:sel];
}
+ (BOOL)resolveClassMethod:(SEL)sel {
return [super resolveClassMethod:sel];
}
Copy the code
forward
- (id)forwardingTargetForSelector:(SEL)aSelector { return [super forwardingTargetForSelector:aSelector]; } the method signature - (NSMethodSignature *) methodSignatureForSelector: (SEL) aSelector {return [super methodSignatureForSelector:aSelector]; } - (void)forwardInvocation:(NSInvocation *)anInvocation {NSLog(@" redirected here "); }Copy the code
The demo address
The code address
Runtime Functions
1. Objc_xxx series of functions
Objc_getMetaClass GetMetaclass objc_allocateClassPair Allocates space and creates classes (member variables can be added only after creation but before registration) Objc_registerClassPair Registers a class that can be used to create objects. Objc_disposeClassPair unregisters a class. Objc_allocateProtocol Specifies the space creation protocol Objc_registerProtocol Registers a protocol objc_constructInstance constructs an instance object (invalid in ARC) objc_destructInstance destructInstance destructObject (invalid in ARC) Objc_setAssociatedObject Associates an instance object objc_getAssociatedObje*ct Obtains the associated object of the instance object objc_removeAssociatedObjects Clears all associated objects of the instance object The objC_ family of functions focuses on macro usage, such as class and protocol space allocation, registration, logout, and other operationsCopy the code
2. Class_xxx series functions
Function Name Function Function class_addIvar Adds an instance variable to a class. Class_addProperty adds an attribute to a class. Class_addMethod adds a method to a class Class_getName Obtains the class name class_isMetaClass Determines whether a protocol objc_copyProtocolList is obtained for the metaclass objc_getProtocol Copy the list of protocols registered at runtime class_getSuperclass Gets the parent of a class class_setSuperclass Sets the parent of a class class_getProperty Gets the properties of a class Class_getInstanceVariable Obtains the instance variable class_getClassVariable Obtains the class variable class_getInstanceMethod Obtains the instance method class_getClassMethod Obtains the class method The implementation of the class_getMethodImplementation method class_getInstanceSize gets the size of the instance of the class class_respondsToSelector determines whether the class implements a method Class_conformsToProtocol Determines whether a class complies with a protocol. Class_createInstance Creates an instance of the class. Class_copyIvarList Copies the list of instance variables of the class Class_copyProtocolList List of protocols that copy a class. Class_copyPropertyList List of properties that copy a class. Class_ functions focus on the inner parts of a class, such as instance variables, properties, methods, and protocolsCopy the code
3.object_xxx series of functions
Object_copy Object copy(ARC invalid) object_Dispose object release (ARC invalid) object_getClassName Obtains the Class name of the object object_getClass Obtains the Class of the object Object_setIvar Sets the value of the instance variable in the object. Object_getInstanceVariable gets the value of the instance variable in the object Object_setInstanceVariable sets the value of an instance variable in an object (invalid in ARC, using object_setIvar). The objcet_ family of functions focuses on the object's perspective, such as instance variablesCopy the code
4. Method_xxx series of functions
Method_getName Gets the name of the method method_getImplementation Implementation of the method method_getTypeEncoding Gets the type encoding of the method Method_getNumberOfArguments Number of method parameters method_copyReturnType Return type of the copied method method_getReturnType Obtain the return type of the method Method_copyArgumentType Copies the argument types of the method method_getArgumentType Gets the argument types of the method method_getDescription Gets the description of the method The method_ family of functions looks inside a method, if the method's parameters and return value types and the method's implementationCopy the code
5. Property_xxx series functions
Function Name Function Function property_getName Obtains the property name property_getAttributes Obtains the property list of the property. Property_copyAttributeList Specifies the property list of the copied property Property_copyAttributeValue Specifies the value of a property in a copy property. Property_ is a class function concerned with the property *, such as the property of the propertyCopy the code
6. Protocol_xxx series functions
Function Name Function Function protocol_conformsToProtocol Determines whether one protocol complies with another protocol_isEqual determines whether two protocols are consistent protocol_getName Obtains the protocol name Protocol_copyPropertyList copy a list of attributes for the protocol protocol_copyProtocolList copy to follow a certain agreement protocol list protocol_copyMethodDescriptionList copy list of the methods Protocol_addProtocol Indicates that a protocol complies with another protocol. Protocol_addProperty Adds an attribute to a protocol. Protocol_getProperty Obtains an attribute of a protocol Protocol_addMethodDescription Adds a method description to a protocol. Protocol_getMethodDescription Gets the description of a method in a protocolCopy the code
Ivar_xxx series of functions
Function Name Function Function ivar_getName Obtains the Ivar name ivar_getTypeEncoding Obtains the type encoding ivar_getOffset Obtains the offsetCopy the code
Sel_xxx series of functions
Function Name Function Function sel_getName Obtains the name sel_getUid registration method sel_registerName Registration method sel_isEqual Determines whether methods are equalCopy the code
9. Imp_xxx series functions
Function name function are imp_implementationWithBlock create IMP through code block imp_getBlock access code block of function Pointers imp_removeBlock remove code block of IMPCopy the code