Skip to content

Instantly share code, notes, and snippets.

@faimin
Created May 17, 2017 10:31
Show Gist options
  • Save faimin/524dc8b18d02ddf9a5f8793ec471d59e to your computer and use it in GitHub Desktop.
Save faimin/524dc8b18d02ddf9a5f8793ec471d59e to your computer and use it in GitHub Desktop.
weak associate
@implementation NSObject (ZDWeakAssociate)
- (void)zd_setWeakAssociateValue:(id)value forKey:(void *)key {
__weak id weakValue = value;
objc_setAssociatedObject(self, key, ^{
return weakValue;
}, OBJC_ASSOCIATION_COPY);
}
- (id)zd_getWeakAssociateValueForKey:(void *)key {
id(^tempBlock)() = objc_getAssociatedObject(self, key);
if (tempBlock) {
return tempBlock();
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment