后面主要介绍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.*/