Skip to content

Instantly share code, notes, and snippets.

@YauzZ
Last active August 24, 2016 03:19
Show Gist options
  • Save YauzZ/a0004327d857ef86612570c6e8696ce7 to your computer and use it in GitHub Desktop.
Save YauzZ/a0004327d857ef86612570c6e8696ce7 to your computer and use it in GitHub Desktop.
使用 NSProxy 来解除引用循环
/**
A proxy used to hold a weak object.
It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
sample code:
@implementation MyView {
NSTimer *_timer;
}
- (void)initTimer {
YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
_timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
}
- (void)tick:(NSTimer *)timer {...}
@end
*/
看到YYKit代码里有这么一段,才知道NSProxy可以这样用,这种解除循环的方式比__weak的形式更优雅些。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment