美文网首页
iOS NSProxy消息转发

iOS NSProxy消息转发

作者: 山杨 | 来源:发表于2021-09-23 11:21 被阅读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

相关文章

网友评论

      本文标题:iOS NSProxy消息转发

      本文链接:https://www.haomeiwen.com/subject/dbongltx.html