Last active
July 17, 2017 06:26
-
-
Save edwardean/e210a05338a161acebf34116285cc6ee to your computer and use it in GitHub Desktop.
HLWeakProxy
This file contains hidden or 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
NS_ASSUME_NONNULL_BEGIN | |
/** | |
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)dealloc { | |
[_timer invalidate]; | |
} | |
- (void)initTimer { | |
_timer = [NSTimer timerWithTimeInterval:0.1 target:[self weakProxy] selector:@selector(tick:) userInfo:nil repeats:YES]; | |
} | |
- (void)tick:(NSTimer *)timer {...} | |
@end | |
*/ | |
@interface HLWeakProxy : NSProxy | |
@property (nullable, nonatomic, weak, readonly) id target; | |
- (id)initWithTarget:(id)target; | |
@end | |
@interface NSObject (HLWeakProxy) | |
- (HLWeakProxy *)weakProxy; | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains hidden or 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
@implementation HLWeakProxy | |
- (id)initWithTarget:(id)target | |
{ | |
_target = target; | |
return self; | |
} | |
- (id)forwardingTargetForSelector:(SEL)selector | |
{ | |
return self.target; | |
} | |
- (void)forwardInvocation:(NSInvocation *)invocation | |
{ | |
void *null = NULL; | |
[invocation setReturnValue:&null]; | |
} | |
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector | |
{ | |
return [NSObject instanceMethodSignatureForSelector:@selector(init)]; | |
} | |
- (BOOL)respondsToSelector:(SEL)aSelector | |
{ | |
return [self.target respondsToSelector:aSelector]; | |
} | |
- (BOOL)isEqual:(id)object | |
{ | |
return [self.target isEqual:object]; | |
} | |
- (NSUInteger)hash | |
{ | |
return [self.target hash]; | |
} | |
- (Class)superclass | |
{ | |
return [self.target superclass]; | |
} | |
- (Class)class | |
{ | |
return [self.target class]; | |
} | |
- (BOOL)isKindOfClass:(Class)aClass | |
{ | |
return [self.target isKindOfClass:aClass]; | |
} | |
- (BOOL)isMemberOfClass:(Class)aClass | |
{ | |
return [self.target isMemberOfClass:aClass]; | |
} | |
- (BOOL)conformsToProtocol:(Protocol *)aProtocol | |
{ | |
return [self.target conformsToProtocol:aProtocol]; | |
} | |
- (BOOL)isProxy | |
{ | |
return YES; | |
} | |
- (NSString *)description | |
{ | |
return [self.target description]; | |
} | |
- (NSString *)debugDescription | |
{ | |
return [self.target debugDescription]; | |
} | |
@end | |
@implementation NSObject (HLWeakProxy) | |
- (HLWeakProxy *)weakProxy | |
{ | |
return [[HLWeakProxy alloc] initWithTarget:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment