Runtime的三种使用方式:
- OC原生底层调用会间接使用runtime
例如:方法的调用实质就是消息的发送 - 调用NSObject的方法,间接调用runtime
例如:
- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
- (BOOL)respondsToSelector:(SEL)aSelector;
- 直接调用runtime api
Runtime 常用API
Working with Classes
//Returns the name of a class.
const char * class_getName(Class cls);
//Returns the superclass of a class.
//获取某类的父类
Class class_getSuperclass(Class cls);
//Returns a Boolean value that indicates whether a class object is a metaclass.
//是否为元类
BOOL class_isMetaClass(Class cls);
//Returns the size of instances of a class.
//获取类的实例大小
size_t class_getInstanceSize(Class cls);
//Returns the Ivar for a specified instance variable of a given class.
//获取实例变量
Ivar class_getInstanceVariable(Class cls, const char *name);
//Returns the Ivar for a specified class variable of a given class.
//获取类变量
Ivar class_getClassVariable(Class cls, const char *name);
//Adds a new instance variable to a class.
//为类添加实例变量
BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types);
//Describes the instance variables declared by a class.
//copy类的实例变量列表
Ivar _Nonnull * class_copyIvarList(Class cls, unsigned int *outCount);
//Returns a property with a given name of a given class.
//获取某类的属性
objc_property_t class_getProperty(Class cls, const char *name);
//Describes the properties declared by a class.
//copy类的属性列表
objc_property_t _Nonnull * class_copyPropertyList(Class cls, unsigned int *outCount);
//Adds a new method to a class with a given name and implementation.
//为类添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types);
//Returns a specified instance method for a given class.
//获取实例方法
Method class_getInstanceMethod(Class cls, SEL name);
//Returns a pointer to the data structure describing a given class method for a given class.
//获取类方法
Method class_getClassMethod(Class cls, SEL name);
//Describes the instance methods implemented by a class.
//copy类的方法列表
Method _Nonnull * class_copyMethodList(Class cls, unsigned int *outCount);
//Replaces the implementation of a method for a given class.
//替换类某方法的实现
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types);
//Returns the function pointer that would be called if a particular message were sent to an instance of a class.
//获取方法的实现
IMP class_getMethodImplementation(Class cls, SEL name);
//Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
//类是否实现某方法
BOOL class_respondsToSelector(Class cls, SEL sel);
//Adds a protocol to a class.
//为类遵循协议
BOOL class_addProtocol(Class cls, Protocol *protocol);
//Adds a property to a class.
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);
//Replace a property of a class.
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount);
//Returns a Boolean value that indicates whether a class conforms to a given protocol.
//判断类是否遵循某协议
BOOL class_conformsToProtocol(Class cls, Protocol *protocol);
//Describes the protocols adopted by a class.
//copy类遵循的协议列表
Protocol * _Nonnull * class_copyProtocolList(Class cls, unsigned int *outCount);
Adding Classes
//Creates a new class and metaclass.
//分配空间,创建类(仅在创建后,注册前可以添加成员变量)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes);
//Destroys a class and its associated metaclass.
//注销类
void objc_disposeClassPair(Class cls);
//Registers a class that was allocated using objc_allocateClassPair.
//注册一个类(注册后可使用该类创建对象)
void objc_registerClassPair(Class cls);
Working with Instances
//Reads the value of an instance variable in an object.
//获取对象中功能实例变量的值
id object_getIvar(id obj, Ivar ivar);
//Sets the value of an instance variable in an object.
//设置对象中功能实例变量的值
void object_setIvar(id obj, Ivar ivar, id value);
//Returns the class name of a given object.
//获取对象的类名
const char * object_getClassName(id obj);
//Returns the class of an object.
//获取对象的Class
Class object_getClass(id obj);
//Sets the class of an object.
//设置对象的Class
Class object_setClass(id obj, Class cls);
Obtaining Class Definitions
//Obtains the list of registered class definitions.
int objc_getClassList(Class _Nonnull *buffer, int bufferCount);
//Creates and returns a list of pointers to all registered class definitions.
Class _Nonnull * objc_copyClassList(unsigned int *outCount);
//Returns the metaclass definition of a specified class.
//获取MetaClass对象
id objc_getMetaClass(const char *name);
Working with Instance Variables
//Returns the name of an instance variable.
//获取类名
const char * ivar_getName(Ivar v);
//Returns the type string of an instance variable.
const char * ivar_getTypeEncoding(Ivar v);
Associative References
//Sets an associated value for a given object using a given key and association policy.
//为实例对象关联对象
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
//Returns the value associated with a given object for a given key.
//获取实例对象关联对象
id objc_getAssociatedObject(id object, const void *key);
Sending Messages
//Sends a message with a simple return value to an instance of a class.
//发送消息
void objc_msgSend(void);
Working with Methods
//Returns the name of a method.
//获取方法名
SEL method_getName(Method m);
//Returns the implementation of a method.
//获取方法实现
IMP method_getImplementation(Method m);
//Returns a string describing a method's parameter and return types.
//获取方法的类型编码
const char * method_getTypeEncoding(Method m);
//Returns a string describing a method's return type.
//copy方法的返回类型
char * method_copyReturnType(Method m);
//Returns a string describing a single parameter type of a method.
//copy方法的参数类型
char * method_copyArgumentType(Method m, unsigned int index);
//Returns by reference a string describing a method's return type.
//获取方法返回类型
void method_getReturnType(Method m, char *dst, size_t dst_len);
//Returns the number of arguments accepted by a method.
//获取方法参数个数
unsigned int method_getNumberOfArguments(Method m);
//Returns by reference a string describing a single parameter type of a method.
//获取方法参数的类型
void method_getArgumentType(Method m, unsigned int index, char *dst, size_t dst_len);
//Returns a method description structure for a specified method.
//获取方法的描述
struct objc_method_description * method_getDescription(Method m);
//Sets the implementation of a method.
//设置方法的实现
IMP method_setImplementation(Method m, IMP imp);
//Exchanges the implementations of two methods.
//替换方法的实现
void method_exchangeImplementations(Method m1, Method m2);
Working with Selectors
//Returns the name of the method specified by a given selector.
const char * sel_getName(SEL sel);
//Registers a method with the Objective-C runtime system, maps the method name to a selector, and returns the selector value.
SEL sel_registerName(const char *str);
//Returns a Boolean value that indicates whether two selectors are equal.
BOOL sel_isEqual(SEL lhs, SEL rhs);
Working with Protocols
//Returns a specified protocol.
//获取某个协议
Protocol * objc_getProtocol(const char *name);
//Returns an array of all the protocols known to the runtime.
//copy运行时注册过的协议列表
Protocol * _Nonnull * objc_copyProtocolList(unsigned int *outCount);
//Creates a new protocol instance.
//分配空间创建协议
Protocol * objc_allocateProtocol(const char *name);
//Registers a newly created protocol with the Objective-C runtime.
//注册一个协议
void objc_registerProtocol(Protocol *proto);
//Adds a registered protocol to another protocol that is under construction.
void protocol_addProtocol(Protocol *proto, Protocol *addition);
//Adds a property to a protocol that is under construction.
void protocol_addProperty(Protocol *proto, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);
//Returns a the name of a protocol.
const char * protocol_getName(Protocol *proto);
//Returns a Boolean value that indicates whether two protocols are equal.
//判断两个协议是否一致
BOOL protocol_isEqual(Protocol *proto, Protocol *other);
//Returns an array of the properties declared by a protocol.
//copy协议的属性列表
objc_property_t _Nonnull * protocol_copyPropertyList(Protocol *proto, unsigned int *outCount);
//Returns the specified property of a given protocol.
//获取协议中的某个属性
objc_property_t protocol_getProperty(Protocol *proto, const char *name, BOOL isRequiredProperty, BOOL isInstanceProperty);
//Returns an array of the protocols adopted by a protocol.
//copy某协议所遵循的协议列表
Protocol * _Nonnull * protocol_copyProtocolList(Protocol *proto, unsigned int *outCount);
//Returns a Boolean value that indicates whether one protocol conforms to another protocol.
//判断一个协议是否遵循另一个协议
BOOL protocol_conformsToProtocol(Protocol *proto, Protocol *other);
Working with Properties
//Returns the name of a property.
//获取属性名
const char * property_getName(objc_property_t property);
//Returns the attribute string of a property.
//获取属性列表
const char * property_getAttributes(objc_property_t property);
//Returns the value of a property attribute given the attribute name.
//copy属性值
char * property_copyAttributeValue(objc_property_t property, const char *attributeName);
//Returns an array of property attributes for a given property.
//copy属性表
objc_property_attribute_t * property_copyAttributeList(objc_property_t property, unsigned int *outCount);
常用API的使用
动态创建类
void eat(){
printf("animal eat");
}
- (void)dynamicClass {
//动态创建类
Class Animal = objc_allocateClassPair([NSObject class], "Animal", 0);
//添加实例变量
NSString *name = @"name";
class_addIvar(Animal, name.UTF8String, sizeof(id), log2(sizeof(id)), @encode(id));
//添加方法
class_addMethod(Animal, @selector(eat), (IMP)eat, "v@:");
//注册类
objc_registerClassPair(Animal);
id dog = [[Animal alloc]init];
[dog setValue:@"Dog" forKey:name];
NSLog(@"name:%@", [dog valueForKey:name]);
[dog performSelector:@selector(eat)];
}
获取属性列表
- (void)getClassInfo {
//获取成员变量列表
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([LPerson class], &count);
for (int i = 0; i < count; i++) {
Ivar var = ivars[i];
const char *name = ivar_getName(var);
NSString *key = [NSString stringWithUTF8String:name];
NSLog(@"key:%@",key);
}
free(ivars);
}
生活如此美好,今天就点到为止。。。