Last active
August 24, 2016 03:19
-
-
Save YauzZ/a0004327d857ef86612570c6e8696ce7 to your computer and use it in GitHub Desktop.
使用 NSProxy 来解除引用循环
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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