Runtime函数库中关于方法Method的函数使用

Runtime 函数库提供了很多函数用于一些操作,本篇主要总结关于方法 Method 的操作函数:

/* 返回指定类的指定实例方法。
 * @param name 要获取的方法的选择器类型。
 * @return 如果指定的类或其父类不包含具有指定选择器的实例方法,则返回 NULL。
 * @note 该函数会去父类中搜索,而 class_copyMethodList() 函数不会去父类中搜索。
 */ 
Method class_getInstanceMethod(Class cls, SEL name);

/* 返回指定类的指定类方法。
 * @return 如果指定的类或其父类不包含具有指定选择器的类方法,则返回 NULL。
 * @note 该函数会去父类中搜索,而 class_copyMethodList() 函数不会去父类中搜索。
 */ 
Method class_getClassMethod(Class cls, SEL name);

/* 获取由类实现的所有实例方法。
 * @param outCount 返回数组的长度;如果为 NULL,则不返回长度。
 * @return 获取类实现的实例方法的类型指针数组;
 *         不包括由父类实现的任何实例方法;必须使用 free() 函数释放数组。
 * 
 *  如果 cls不实现实例方法,或者 cls 为Nil,则返回 NULL 且*outCount 值为 0。
 * 
 * @note 要获取类的类方法,使用 class_copyMethodList(object_getClass(cls), &count).
 * @note 要获得父类可能实现的方法的实现,可以使用 class_getInstanceMethod() 函数 或者  class_getClassMethod() 函数.
 */ 
Method *class_copyMethodList(Class cls, unsigned int *outCount);

1、使用方法


/* 获取方法的选择器
 */
SEL _Nonnull method_getName(Method _Nonnull m);

/* 获取方法的函数地址。
 */
IMP _Nonnull method_getImplementation(Method _Nonnull m);

/* 获取方法的参数类型和返回值类型;可能为 NULL
 */
const char * _Nullable method_getTypeEncoding(Method _Nonnull m);

/* 获取方法的参数数量。
 */
unsigned int method_getNumberOfArguments(Method _Nonnull m);

/* 获取方法的返回值类型
 * @return 描述返回类型的C字符串。必须使用 free() 函数释放字符串。
 */
char * _Nonnull method_copyReturnType(Method _Nonnull m);

/* 获取方法的中指定索引位置的参数类型
 * @param index 参数的索引。
 * @return 一个C字符串,描述在索引索引处的参数类型;如果方法没有参数索引,则为NULL;必须使用free()释放字符串。
 */
char * _Nullable method_copyArgumentType(Method _Nonnull m, unsigned int index);

/* 获取返回值类型。
 * @param dst 存储描述的引用字符串
 * @param dst_len 可以存储在dst中的最大字符数。
 * @note 方法的返回类型字符串复制到dst;就像调用 strncpy(dst, parameter_type, dst_len) 一样填充dst。
 */
void method_getReturnType(Method _Nonnull m, char * _Nonnull dst, size_t dst_len);

/* 获取方法的中指定索引位置的参数类型
 * @param index 参数的索引。
 * @param dst 存储描述的引用字符串。
 * @param dst_len 可以存储在dst中的最大字符数。
 * @note 参数类型字符串复制到 dst ;就像调用 strncpy(dst, parameter_type, dst_len) 一样填充d st。
 *       如果该方法不包含具有该索引的参数,则将 dst 填充为调用 strncpy(dst, "", dst_len)。
 */
void method_getArgumentType(Method _Nonnull m, unsigned int index,char * _Nullable dst, size_t dst_len);

/* 获取方法的描述结构体
 */
struct objc_method_description * _Nonnull method_getDescription(Method _Nonnull m);

/* 设置方法的实现。
 * @return 返回该方法的原有 IMP
 */
IMP _Nonnull method_setImplementation(Method _Nonnull m, IMP _Nonnull imp);

/* 交换两个方法的实现。
 * @note 以下代码是细微实现:
 *      IMP imp1 = method_getImplementation(m1);
 *      IMP imp2 = method_getImplementation(m2);
 *      method_setImplementation(m1, imp2);
 *      method_setImplementation(m2, imp1);
 */
void method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2);

2、使用选择器


/* 获取指定选择器的对应方法的名称。
 * @param sel 选择器类型 SEL
 * @return 选择器名称
 */
const char * _Nonnull sel_getName(SEL _Nonnull sel);

/* 向 Objective-C 运行时系统注册一个方法,将方法名映射到选择器,并返回选择器值。
 * @param str 要注册的方法的名称
 * @return 指定方法的选择器。
 * @note 在将方法添加到类定义之前,必须向 Objective-C 运行时系统注册方法名,以获得方法的选择器。
 *       如果方法名已经注册,则该函数只返回选择器。
 */
SEL _Nonnull sel_registerName(const char * _Nonnull str);

/* 判断两个选择器是否相等。
 * @return 如果相等则为YES,否则为NO。
 * @note 该函数等价于 ==
 */
BOOL sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs);

/* 判断选择器为有效或无效。
 * @param sel 要标识的选择器。
 * @return 如果选择器有效且具有函数实现,则为YES,否则为NO。
 * @warning 在某些平台上,无效引用(对无效内存地址的访问)可能导致崩溃。
 */
BOOL sel_isMapped(SEL _Nonnull sel);

/* 向Objective-C运行时系统注册一个方法名。
 * @param str 注册的方法的名称
 * @return 指定方法的选择器。
 * @note 该方法的实现与 sel_registerName() 函数的实现相同。
 * @note 在 OS X version 10.0之前,该函数尝试查找映射到给定名称的选择器,如果没有找到选择器,则返回NULL。
 *       为了安全起见,更改了这个值,因为观察到这个函数的许多调用者没有检查返回值是否为NULL。
 */
SEL _Nonnull sel_getUid(const char * _Nonnull str);

猜你喜欢

转载自blog.csdn.net/weixin_34408717/article/details/87239041