Created
May 17, 2017 10:31
-
-
Save faimin/524dc8b18d02ddf9a5f8793ec471d59e to your computer and use it in GitHub Desktop.
weak associate
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 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