美文网首页
[iOS][Runtime]--class_addMethod参

[iOS][Runtime]--class_addMethod参

作者: 阿不不不不 | 来源:发表于2018-10-16 11:43 被阅读102次

class_addMethod()为类添加方法的参数解析

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

作用:这个方法的作用是,给类添加一个新的方法和该方法的具体实现

BOOL: 返回值,yes-------方法添加成功     no--------方法添加失败

Class cls: 将要给添加方法的类,传的类型 [类名  class]

SEL name: 将要添加的方法名,传的类型   @selector(方法名)

IMP imp:实现这个方法的函数 ,传的类型   1,C语言写法:(IMP)方法名    2,OC的写法:class_getMethodImplementation(self,@selector(方法名:))

const char *types:表示我们要添加的方法的返回值和参数

"v@:@":v:是添加方法无返回值    @表示是id(也就是要添加的类) :表示添加的方法类型  @表示:参数类型

const char *types含义表:

还有一点需要注意,用这个方法添加的方法是无法直接调用的,必须用performSelector:调用。

因为performSelector是运行时系统负责去找方法的,在编译时候不做任何校验;如果直接调用编译是会自动校验。

你添加方法是在运行时添加的,你在编译的时候还没有这个本类方法,所以当然不行。

相关文章

网友评论

      本文标题:[iOS][Runtime]--class_addMethod参

      本文链接:https://www.haomeiwen.com/subject/zjavrttx.html