Working with Instanceshtml
// 返回指定对象的一份拷贝 id _Nullable object_copy(id _Nullable obj, size_t size); // 释放指定对象占用的内存 id _Nullable object_dispose(id _Nullable obj); // 返回对象的类 Class _Nullable object_getClass(id _Nullable obj); // 设置对象的类 Class _Nullable object_setClass(id _Nullable obj, Class _Nonnull cls); // 返回指定对象是不是一个类对象 BOOL object_isClass(id _Nullable obj); // 已知Ivar,获取对象中指定成员变量的值 id _Nullable object_getIvar(id _Nullable obj, Ivar _Nonnull ivar); // 已知Ivar,设置对象中指定成员变量的值 void object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value); // 已知Ivar,设置对象中指定成员变量的值(强引用) void object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value); // 设置对象中指定成员变量的值 Ivar _Nullable object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name, void * _Nullable value); // 设置对象中指定成员变量的值(强引用) Ivar _Nullable object_setInstanceVariableWithStrongDefault(id _Nullable obj, const char * _Nonnull name, void * _Nullable value); // 获取对象中指定成员变量的值 Ivar _Nullable object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name, void * _Nullable * _Nullable outValue);
Obtaining Class Definitionsios
// 经过类名字符串获取类(若是类未在runtime中注册,则调用类的处理回调,并再次确认类是否注册,若是确认未注册,再返回nil) Class _Nullable objc_getClass(const char * _Nonnull name); // 经过类名字符串获取类(若是类未在runtime中注册则返回nil) Class _Nullable objc_lookUpClass(const char * _Nonnull name); // 经过类名字符串获取类(若是找不到该类则杀死进程) Class _Nonnull objc_getRequiredClass(const char * _Nonnull name); // 经过类名字符串获取该类的元类 Class _Nullable objc_getMetaClass(const char * _Nonnull name); // 获取已注册的类定义列表 int objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount); // 建立并返回一个指向全部已注册类的指针列表 Class _Nonnull * _Nullable objc_copyClassList(unsigned int * _Nullable outCount);
Working with Classessegmentfault
// 获取类的类名 const char * _Nonnull class_getName(Class _Nullable cls); // 判断一个是不是元类 BOOL class_isMetaClass(Class _Nullable cls); // 获取类的父类 Class _Nullable class_getSuperclass(Class _Nullable cls); // 设置一个类的父类 Class _Nonnull class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper); // 获取类版本号 int class_getVersion(Class _Nullable cls); // 设置类版本号 void class_setVersion(Class _Nullable cls, int version); // 获取实例大小 size_t class_getInstanceSize(Class _Nullable cls); // 获取类中指定名称实例成员变量的信息 Ivar _Nullable class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name); // 获取类成员变量的信息 Ivar _Nullable class_getClassVariable(Class _Nullable cls, const char * _Nonnull name); // 获取成员变量列表 Ivar _Nonnull * _Nullable class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount); // 获取实例方法 Method _Nullable class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name); // 获取类方法 Method _Nullable class_getClassMethod(Class _Nullable cls, SEL _Nonnull name); // 返回方法的具体实现 IMP _Nullable class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name); IMP _Nullable class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name); // 类实例是否响应指定的selector BOOL class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel); // 获取全部方法列表 Method _Nonnull * _Nullable class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount); // 返回类是否实现指定的协议 BOOL class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol); // 返回类实现的协议列表 Protocol * __unsafe_unretained _Nonnull * _Nullable class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount); // 获取指定的属性 objc_property_t _Nullable class_getProperty(Class _Nullable cls, const char * _Nonnull name); // 获取属性列表 objc_property_t _Nonnull * _Nullable class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount); // 添加方法 BOOL class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types); // 替换方法的实现 IMP _Nullable class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types); // 添加成员变量 BOOL class_addIvar(Class _Nullable cls, const char * _Nonnull name, size_t size, uint8_t alignment, const char * _Nullable types); // 添加协议 BOOL class_addProtocol(Class _Nullable cls, Protocol * _Nonnull protocol); // 为类添加属性 BOOL class_addProperty(Class _Nullable cls, const char * _Nonnull name, const objc_property_attribute_t * _Nullable attributes, unsigned int attributeCount); // 替换类的属性 void class_replaceProperty(Class _Nullable cls, const char * _Nonnull name, const objc_property_attribute_t * _Nullable attributes, unsigned int attributeCount); // 肯定一个对象的内存区域是否能够被垃圾回收器扫描,以处理strong/weak引用,无需主动调用 const uint8_t * _Nullable class_getIvarLayout(Class _Nullable cls); const uint8_t * _Nullable class_getWeakIvarLayout(Class _Nullable cls); void class_setIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout); void class_setWeakIvarLayout(Class _Nullable cls, const uint8_t * _Nullable layout); // 提供给CoreFoundation的tool-free bridging使用的方法 Class _Nonnull objc_getFutureClass(const char * _Nonnull name);
Instantiating Classes数组
// 建立类实例 id _Nullable class_createInstance(Class _Nullable cls, size_t extraBytes); // 在指定位置建立类实例 id _Nullable objc_constructInstance(Class _Nullable cls, void * _Nullable bytes); // 销毁类实例 void * _Nullable objc_destructInstance(id _Nullable obj);
Adding Classes框架
// 建立一个新类和元类 Class _Nullable objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, size_t extraBytes); // 在应用中注册由objc_allocateClassPair建立的类 void objc_registerClassPair(Class _Nonnull cls); // 用于KVO,不要本身调用 Class _Nonnull objc_duplicateClass(Class _Nonnull original, const char * _Nonnull name, size_t extraBytes); // 销毁一个类及其相关联的类 void objc_disposeClassPair(Class _Nonnull cls);
Working with Methods函数
// 获取方法名 SEL _Nonnull method_getName(Method _Nonnull m); // 获取方法实现 IMP _Nonnull method_getImplementation(Method _Nonnull m); // 获取方法的属性信息(包括返回值类型,参数类型等等信息) const char * _Nullablemethod_getTypeEncoding(Method _Nonnull m); // 获取方法的参数个数 unsigned int method_getNumberOfArguments(Method _Nonnull m); // 获取参数的属性参数 char * _Nullable method_copyArgumentType(Method _Nonnull m, unsigned int index); void method_getArgumentType(Method _Nonnull m, unsigned int index, char * _Nullable dst, size_t dst_len); // 获取返回值的类型 void method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len); char * _Nonnull method_copyReturnType(Method _Nonnull m); // 设置一个方法的实现 IMP _Nonnull method_setImplementation(Method _Nonnull m, IMP _Nonnull imp); // 交换两个方法的实现 void method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2);
Working with Instance Variablesui
// 获取变量名 const char * _Nullable ivar_getName(Ivar _Nonnull v); // 获取变量类型描述 const char * _Nullable ivar_getTypeEncoding(Ivar _Nonnull v); // 获取变量基地址偏移量 ptrdiff_t ivar_getOffset(Ivar _Nonnull v);
Working with Properties指针
// 获取属性名 const char * _Nonnull property_getName(objc_property_t _Nonnull property); // 获取属性的属性 const char * _Nullable property_getAttributes(objc_property_t _Nonnull property); // 获取属性的属性列表 objc_property_attribute_t * _Nullable property_copyAttributeList(objc_property_t _Nonnull property, unsigned int * _Nullable outCount); // 获取属性的属性值 char * _Nullable property_copyAttributeValue(objc_property_t _Nonnull property, const char * _Nonnull attributeName);
Working with Protocolscode
// 根据字符串获取协议 Protocol * _Nullable objc_getProtocol(const char * _Nonnull name); // 获取runtime中已知的全部协议列表 Protocol * __unsafe_unretained _Nonnull * _Nullable objc_copyProtocolList(unsigned int * _Nullable outCount); // 返回一个协议是否遵照另外一个协议。 BOOL protocol_conformsToProtocol(Protocol * _Nullable proto, Protocol * _Nullable other); // 返回两个协议是否相等 BOOLprotocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other); // 获取协议名 const char * _Nonnull protocol_getName(Protocol * _Nonnull proto); // 根据给定的协议、方法选择器和要求返回方法描述 struct objc_method_description protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel, BOOL isRequiredMethod, BOOL isInstanceMethod); // 返回协议中符合给定要求的方法描述数组 struct objc_method_description * _Nullable protocol_copyMethodDescriptionList(Protocol * _Nonnull proto, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int * _Nullable outCount); // 获取协议的指定属性 objc_property_t _Nullable protocol_getProperty(Protocol * _Nonnull proto, const char * _Nonnull name, BOOL isRequiredProperty, BOOL isInstanceProperty); // 获取协议中的属性列表 objc_property_t _Nonnull * _Nullable protocol_copyPropertyList(Protocol * _Nonnull proto, unsigned int * _Nullable outCount); objc_property_t _Nonnull * _Nullable protocol_copyPropertyList2(Protocol * _Nonnull proto, unsigned int * _Nullable outCount, BOOL isRequiredProperty, BOOL isInstanceProperty); // 获取协议遵照的全部协议 Protocol * __unsafe_unretained _Nonnull * _Nullable protocol_copyProtocolList(Protocol * _Nonnull proto, unsigned int * _Nullable outCount); // 建立新的协议 Protocol * _Nullable objc_allocateProtocol(const char * _Nonnull name); // 在运行时中注册新建立的协议 void objc_registerProtocol(Protocol * _Nonnull proto); // 为协议添加方法 void protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name, const char * _Nullable types, BOOL isRequiredMethod, BOOL isInstanceMethod); // 添加一个已注册的协议到协议中 void protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition); // 为协议添加属性 void protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name, const objc_property_attribute_t * _Nullable attributes, unsigned int attributeCount, BOOL isRequiredProperty, BOOL isInstanceProperty);
Working with Librariesorm
// 获取全部加载的Objective-C框架和动态库的名称 const char * _Nonnull * _Nonnull objc_copyImageNames(unsigned int * _Nullable outCount); // 获取指定类所在动态库 const char * _Nullable class_getImageName(Class _Nullable cls); // 获取指定库或框架中全部类的类名 const char * _Nonnull * _Nullable objc_copyClassNamesForImage(const char * _Nonnull image, unsigned int * _Nullable outCount);
Working with Selectors
// 获取方法选择器的名字 const char * _Nonnull sel_getName(SEL _Nonnull sel); // 向运行时系统注册一个方法选择器 SEL _Nonnull sel_registerName(const char * _Nonnull str); // 判断两个方法选择器是否相等 BOOL sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs);
Objective-C Language Features
// 在迭代期间检测到遍历对象发生了改变,则会插入该函数抛出异常 void objc_enumerationMutation(id _Nonnull obj); // 设置出现上述异常时的处理块 void objc_setEnumerationMutationHandler(void (*_Nullable handler)(id _Nonnull )); // 设置objc_msgForward的回调方法 void objc_setForwardHandler(void * _Nonnull fwd, void * _Nonnull fwd_stret); // 建立一个指针函数的指针,该函数调用时会调用特定的block IMP _Nonnull imp_implementationWithBlock(id _Nonnull block); // 返回与IMP(使用imp_implementationWithBlock建立的)相关的block id _Nullable imp_getBlock(IMP _Nonnull anImp); // 解除block与IMP(使用imp_implementationWithBlock建立的)的关联关系,并释放block的拷贝 BOOL imp_removeBlock(IMP _Nonnull anImp); // 加载弱引用指针引用的对象并返回 id _Nullable objc_loadWeak(id _Nullable * _Nonnull location); // 存储__weak变量的新值 id _Nullable objc_storeWeak(id _Nullable * _Nonnull location, id _Nullable obj);
Associative References
// 为对象设置关联对象 void objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key, id _Nullable value, objc_AssociationPolicy policy); // 获取对象的关联对象 id _Nullable objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key); // 移除对象的全部关联对象 void objc_removeAssociatedObjects(id _Nonnull object);
Hooks for Swift
/* Hooks for Swift */ // 拦截class_getImageName()的hook方法类型 typedef BOOL (*objc_hook_getImageName)(Class _Nonnull cls, const char * _Nullable * _Nonnull outImageName); // 给class_getImageName()安装一个hook方法 void objc_setHook_getImageName(objc_hook_getImageName _Nonnull newValue,objc_hook_getImageName _Nullable * _Nonnull outOldValue)