• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c 编程总结(第六篇)运行时操作 - 方法交换

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

后面主要介绍oc类的运行时行为。这里面包括运行时方法的更换,消息的转发,以及动态属性。这些对于面向方面编程AOP的热爱者还是很有用的,当然还有很多其他的作用,例如可配置编程之类的。但是按照我之前在java和dotnet的编程经验,这些都是使用性能为代价的。所以尽量在程序开始部分完成操作,而不是用于程序行为的代码。

第一段代码是方法交换。下面的例子将使用自己的代码替换[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—self and _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_addMethod were 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_setImplementation were called. The type encoding specified by types is ignored.

*/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-CRuntime初探:selfsuper发布时间:2022-07-12
下一篇:
神经病院Objective-C Runtime出院第三天——如何正确使用Runtime发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap