NSProxy的学习使用
NSProxy
一个抽象的超类,并且是不继承自NSObject的。可以通过它的API为其它的Object对象或者不存在的对象提供替身。
大概意思就是:我们知道狗不能飞,但是如果你通过NSProxy强行调用[狗 飞]的时候,可以通过NSProxy的提供的API,让狗飞的这个操作交给鸟来执行,前提是NSProxy持有了鸟的Target。
举个猫吃狗叫的例子,参考的是apple的文档。
#import <Foundation/Foundation.h>
@interface ProxyManager : NSProxy
+ (id)proxyForObject1:(id)obj1 object2:(id)obj2;
@end
#import "ProxyManager.h"
@interface ProxyManager()
@property (nonatomic, weak) id target1;
@property (nonatomic, weak) id target2;
@end
@implementation ProxyManager
- (instancetype)initWithTarget1:(id)obj1 target2:(id)obj2 {
_target1 = obj1;
_target2 = obj2;
return self;
}
+ (id)proxyForObject1:(id)obj1 object2:(id)obj2 {
return [[ProxyManager alloc] initWithTarget1:obj1 target2:obj2];
}
//重写NSProxy如下两个方法,在处理消息转发时,将消息转发给真正的Target处理
- (void)forwardInvocation:(NSInvocation *)invocation {
id target;
if ([_target1 methodSignatureForSelector:invocation.selector]) {
target = _target1;
}else{
target = _target2;
}
[invocation invokeWithTarget:target];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
NSMethodSignature *sig;
sig = [_target1 methodSignatureForSelector:selector];
if (sig) {
return sig;
}
sig = [_target2 methodSignatureForSelector:selector];
return sig;
}
@end
测试代码:
Dog *dog = [Dog new];
Cat *cat = [Cat new];
id proxy = [ProxyManager proxyForObject1:dog object2:cat];
[proxy speak];
[proxy eat];
[proxy speak];
执行结果:
speak dog
eat Cat
speak dog