NSProxy专门用于处理消息转发

NSProxy相比于NSObject处理消息转发效率更高,NSObject需要走发消息、动态方法解析、然后才到消息转发阶段,NSProxy会直接来到消息转发methodSignatureForSelector,

@interface Myproxy : NSProxy

+ (instancetype)proxyWithTarget:(id)target;
@property (nonatomic, weak) id target;

@end
#import "Myproxy.h"

@implementation Myproxy

+ (instancetype)proxyWithTarget:(id)target
{
    // NSProxy对象不需要调用init,因为它本来就没有init方法
    Myproxy *proxy = [Myproxy alloc];
    proxy.target = target;
    return proxy;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel{
    return [self.target methodSignatureForSelector:sel];
}

- (void)forwardInvocation:(NSInvocation *)invocation{
    [invocation invokeWithTarget:self.target];
}

@end

 

posted @ 2021-04-08 10:22  zk1947  阅读(111)  评论(0编辑  收藏  举报