iOS NSProxy消息转发

2021-09-23  本文已影响0人  山杨

NSProxy的消息转发机制,实现原理在GNUstep Base

日常使用中用来处理NSTimer以及CADisplayLink的循环引用的问题

NSTimer为例

YSProxy *proxy = [YSProxy alloc];// NSProxy中没有init方法
proxy.target = self;

self.timer = [NSTimer scheduledTimerWithTimeInterval:0.8f target:proxy selector:@selector(testProxy) userInfo:nil repeats:YES];

YSProxy

@interface YSProxy : NSProxy

@property (nonatomic, weak) id target;

@end
@implementation YSProxy

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

- (void)forwardInvocation:(NSInvocation *)invocation {
    
    [invocation invokeWithTarget:self.target];
}
@end
上一篇 下一篇

猜你喜欢

热点阅读