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
网友评论