objective-c 编程总结(第六篇)运行时操作 - 方法交换
2012-03-13 16:20
183 查看
后面主要介绍oc类的运行时行为。这里面包括运行时方法的更换,消息的转发,以及动态属性。这些对于面向方面编程AOP的热爱者还是很有用的,当然还有很多其他的作用,例如可配置编程之类的。但是按照我之前在java和dotnet的编程经验,这些都是使用性能为代价的。所以尽量在程序开始部分完成操作,而不是用于程序行为的代码。
第一段代码是方法交换。下面的例子将使用自己的代码替换[NSString stringByAppendingPathComponent]方法的实现。
这里是替换代码:
下面是方法交换的代码:
Class strcls = [NSString class];
SEL oriStringByAppendingPathComponent = @selector(stringByAppendingPathComponent:);
class_replaceMethod(strcls,
oriStringByAppendingPathComponent,
(IMP)NSStringstringByAppendingPathComponent,
NULL);
//后面的type参数可以是NULL。如果要替换的方法不存在,那么class_addMethod会被调用,type参数将被用来设置被添加的方法
/*
Apple development reference 的描述如下:
type参数:An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—
If the method identified by name does not yet exist, it is added as if
If the method identified by name does exist, its IMP is replaced as if
*/
第一段代码是方法交换。下面的例子将使用自己的代码替换[NSString stringByAppendingPathComponent]方法的实现。
这里是替换代码:
NSString * NSStringstringByAppendingPathComponent(id SELF, SEL _cmd, NSString * path){ //开发文档推荐的是这种定义形式,其实在方法交换场景下,这是没有必要的,你甚至可以给一个().但是如果你要替换的方法实际并不存在,那么这个定义形式是必须的。 NSLog(@”this is a fake imp for method %@”, NSStringFromSelctor(_cmd)); NSLog(@”I won’t do anything! but I will return a virus!”);//疯狂医生的逻辑 return [NSString stringWithCString: “virus!!!” encoding:NSUTF8StringEncoding]; }
下面是方法交换的代码:
Class strcls = [NSString class];
SEL oriStringByAppendingPathComponent = @selector(stringByAppendingPathComponent:);
class_replaceMethod(strcls,
oriStringByAppendingPathComponent,
(IMP)NSStringstringByAppendingPathComponent,
NULL);
//后面的type参数可以是NULL。如果要替换的方法不存在,那么class_addMethod会被调用,type参数将被用来设置被添加的方法
/*
Apple development reference 的描述如下:
type参数:An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—
selfand
_cmd, the second and third characters must be “
@:” (the first character is the return type).
If the method identified by name does not yet exist, it is added as if
class_addMethodwere called. The type encoding specified by types is used as given.
If the method identified by name does exist, its IMP is replaced as if
method_setImplementationwere called. The type encoding specified by types is ignored.
*/
相关文章推荐
- objective-c 编程总结(第九篇)运行时操作 - 序列化
- objective-c 编程总结(第九篇)运行时操作 - 序列化
- objective-c 编程总结(第八篇)运行时操作 - 消息转发
- objective-c 编程总结(第七篇)运行时操作 - 动态属性
- objective-c 编程总结(第八篇)运行时操作 - 消息转发
- objective-c 编程总结(第四篇)日期型类型操作
- Objective-C 时间操作实用方法总结
- objective-c 编程总结(第五篇)集合类型操作
- Objective-c读取沙盒中文件的方法总结和NSFileManager主要操作
- C编程 编译运行过程中系统提示错误及解决方法总结
- Objective-C运行时编程 - 方法混写 Method Swizzling
- Objective-C运行时编程 - 实现自动化description方法的思路及代码示例
- VBA编程“由于另一个程序正在运行中,此操作无法完成.”解决方法.
- 001_你使用过Objective-C的运行时编程(Runtime Programming)么?如果使用过,你用它做了什么?你还能记得你所使用的相关的头文件或者某些方法的名称吗?
- Objective-C运行时编程指南之动态方法决议
- POI操作Excel常用方法总结
- Objective-C 2.0的运行时编程
- Objective-C Runtime 运行时之三:方法与消息
- C#操作Excel(创建、打开、读写、保存)几种方法的总结
- VC操作Excel文件编程相关内容总结