天天省钱快报

YYWeakProxy.h 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. NS_ASSUME_NONNULL_BEGIN
  13. /**
  14. A proxy used to hold a weak object.
  15. It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
  16. sample code:
  17. @implementation MyView {
  18. NSTimer *_timer;
  19. }
  20. - (void)initTimer {
  21. YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self];
  22. _timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
  23. }
  24. - (void)tick:(NSTimer *)timer {...}
  25. @end
  26. */
  27. @interface YYWeakProxy : NSProxy
  28. /**
  29. The proxy target.
  30. */
  31. @property (nullable, nonatomic, weak, readonly) id target;
  32. /**
  33. Creates a new weak proxy for target.
  34. @param target Target object.
  35. @return A new proxy object.
  36. */
  37. - (instancetype)initWithTarget:(id)target;
  38. /**
  39. Creates a new weak proxy for target.
  40. @param target Target object.
  41. @return A new proxy object.
  42. */
  43. + (instancetype)proxyWithTarget:(id)target;
  44. @end
  45. NS_ASSUME_NONNULL_END