YYWeakProxy.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // YYWeakProxy.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/18.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <Foundation/Foundation.h>
  12. /**
  13. A proxy used to hold a weak object.
  14. It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
  15. sample code:
  16. @implementation MyView {
  17. NSTimer *_timer;
  18. }
  19. - (void)initTimer {
  20. YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
  21. _timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
  22. }
  23. - (void)tick:(NSTimer *)timer {...}
  24. @end
  25. */
  26. @interface YYWeakProxy : NSProxy
  27. /**
  28. The proxy target.
  29. */
  30. @property (nonatomic, weak, readonly) id target;
  31. /**
  32. Creates a new weak proxy for target.
  33. @param target Target object.
  34. @return A new proxy object.
  35. */
  36. - (instancetype)initWithTarget:(id)target;
  37. /**
  38. Creates a new weak proxy for target.
  39. @param target Target object.
  40. @return A new proxy object.
  41. */
  42. + (instancetype)proxyWithTarget:(id)target;
  43. @end