objc/runtime中SEL、IMP和method动态定义
来源:互联网 发布:sjcam sj5000x淘宝 编辑:程序博客网 时间:2024/06/03 13:24
定义
先来看一下method
相关的定义
typedef struct objc_method *Method;typedef struct objc_selector *SEL;typedef void (*IMP)(void /* id, SEL, ... */ ); //方法描述struct objc_method_description {SEL name; //方法名称char *types; //参数类型字符串};//以下代码是 ObjC2.0 之前method的定义struct objc_method { SEL method_name; char *method_types; IMP method_imp;}
里边有三个类型别名,在这儿先解释一下
SEL
selector的简写,俗称方法选择器,实质存储的是方法的名称IMP
implement的简写,俗称方法实现,看源码得知它就是一个函数指针Method
对上述两者的一个包装结构.
函数
method相关的函数也不是太多,下边简单罗列说明一下
//判断类中是否包含某个方法的实现BOOL class_respondsToSelector(Class cls, SEL sel)//获取类中的方法列表Method *class_copyMethodList(Class cls, unsigned int *outCount) //为类添加新的方法,如果方法该方法已存在则返回NOBOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)//替换类中已有方法的实现,如果该方法不存在添加该方法IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types) //获取类中的某个实例方法(减号方法)Method class_getInstanceMethod(Class cls, SEL name)//获取类中的某个类方法(加号方法)Method class_getClassMethod(Class cls, SEL name)//获取类中的方法实现IMP class_getMethodImplementation(Class cls, SEL name)//获取类中的方法的实现,该方法的返回值类型为structIMP class_getMethodImplementation_stret(Class cls, SEL name) //获取Method中的SELSEL method_getName(Method m) //获取Method中的IMPIMP method_getImplementation(Method m)//获取方法的Type字符串(包含参数类型和返回值类型)const char *method_getTypeEncoding(Method m) //获取参数个数unsigned int method_getNumberOfArguments(Method m)//获取返回值类型字符串char *method_copyReturnType(Method m)//获取方法中第n个参数的Typechar *method_copyArgumentType(Method m, unsigned int index)//获取Method的描述struct objc_method_description *method_getDescription(Method m)//设置Method的IMPIMP method_setImplementation(Method m, IMP imp) //替换Methodvoid method_exchangeImplementations(Method m1, Method m2)//获取SEL的名称const char *sel_getName(SEL sel)//注册一个SELSEL sel_registerName(const char *str)//判断两个SEL对象是否相同BOOL sel_isEqual(SEL lhs, SEL rhs) //通过块创建函数指针,block的形式为^ReturnType(id self,参数,...)IMP imp_implementationWithBlock(id block)//获取IMP中的blockid imp_getBlock(IMP anImp)//移出IMP中的blockBOOL imp_removeBlock(IMP anImp)//调用target对象的sel方法id objc_msgSend(id target, SEL sel, 参数列表...)
Showtime
下边就来玩玩runtime API中的method相关操作
//创建继承自NSObject类的People类Class People = objc_allocateClassPair([NSObject class], "People", 0);//将People类注册到runtime中objc_registerClassPair(People);//注册test: 方法选择器SEL sel = sel_registerName("test:");//函数实现IMP imp = imp_implementationWithBlock(^(id this,id args,...){ NSLog(@"方法的调用者为 %@",this); NSLog(@"参数为 %@",args); return @"返回值测试";});//向People类中添加 test:方法;函数签名为@@:@,// 第一个@表示返回值类型为id,// 第二个@表示的是函数的调用者类型,// 第三个:表示 SEL// 第四个@表示需要一个id类型的参数class_addMethod(People, sel, imp, "@@:@");//替换People从NSObject类中继承而来的description方法class_replaceMethod(People, @selector(description), imp_implementationWithBlock(^NSString*(id this,...){ return @"我是Person类的对象";}), "@@:");//完成 [[People alloc]init];id p1 = objc_msgSend(objc_msgSend(People, @selector(alloc)),@selector(init));//调用p1的sel选择器的方法,并传递@"???"作为参数id result = objc_msgSend(p1, sel,@"???");//输出sel方法的返回值NSLog(@"sel 方法的返回值为 : %@",result);//获取People类中实现的方法列表NSLog(@"输出People类中实现的方法列表");unsigned int methodCount;Method * methods = class_copyMethodList(People, &methodCount);for (int i = 0; i<methodCount; i++) { NSLog(@"方法名称:%s",sel_getName(method_getName(methods[i]))); NSLog(@"方法Types:%s",method_getDescription(methods[i])->types);}free(methods);
输出
2014-08-20 00:03:38.496 Test[99505:303] 方法的调用者为 我是Person类的对象2014-08-20 00:03:38.498 Test[99505:303] 参数为 ???2014-08-20 00:03:38.498 Test[99505:303] sel 方法的返回值为 : 返回值测试2014-08-20 00:03:38.499 Test[99505:303] 输出People类中实现的方法列表2014-08-20 00:03:38.499 Test[99505:303] 方法名称:description2014-08-20 00:03:38.499 Test[99505:303] 方法Types:@@:2014-08-20 00:03:38.500 Test[99505:303] 方法名称:test:2014-08-20 00:03:38.500 Test[99505:303] 方法Types:@@:@
- 顶
- 0
0 0
- objc/runtime中SEL、IMP和method动态定义
- objc/runtime中SEL、IMP和method动态定义
- iOS中SEL和IMP
- Method,SEL,IMP
- runtime运行时 isa指针 SEL方法选择器 IMP函数指针 Method方法 runtime消息机制 runtime的使用
- iOS Runtime详解之SEL,Class,id,IMP,_cmd,isa,method,Ivar
- iOS Runtime详解之SEL,Class,id,IMP,_cmd,isa,method,Ivar
- SEL和IMP
- SEL和IMP
- SEL和IMP
- SEL、IMP和Class
- IMP 和 SEL 区别
- SEL和IMP
- Runtime--Selector、IMP、Method
- Objective-c中的SEL、Method、IMP
- objc runtime 动态增加属性
- objc runtime 动态增加属性
- objc runtime 动态增加属性
- 安卓强大的Fragment管理器
- gulp + webpack 构建多页面前端项目
- 基于 webpack 搭建前端工程基础篇
- uva 10288 Coupons 数学期望
- codeforces 706D (字典树)
- objc/runtime中SEL、IMP和method动态定义
- [React项目总结] 基于 webpack 搭建前端工程基础篇
- 在多个Android项目工程中共享一个库模块
- webpack & react项目搭建一:环境
- android数据存储之文件存储
- QFtp多路并发传输崩溃的问题
- 动态规划问题系列---连续子数组(二维)的最大和
- PAT 1041. Be Unique (20)(判断第一个不重复出现的数字是哪个)
- 使用Win+JKLI实现上下左右的AutoHotKey脚本