Last active
April 24, 2019 02:54
-
-
Save 623637646/3947d2f460decd0d12643f82c0424943 to your computer and use it in GitHub Desktop.
Objective C weak associated
This file contains 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
- (id)weakObject { | |
id (^block)() = objc_getAssociatedObject(self, @selector(weakObject)); | |
return (block ? block() : nil); | |
} | |
- (void)setWeakObject:(id)object { | |
id __weak weakObject = object; | |
id (^block)() = ^{ return weakObject; }; | |
objc_setAssociatedObject(self, @selector(weakObject), | |
block, OBJC_ASSOCIATION_COPY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment