Skip to content

Instantly share code, notes, and snippets.

@623637646
Last active April 24, 2019 02:54
Show Gist options
  • Save 623637646/3947d2f460decd0d12643f82c0424943 to your computer and use it in GitHub Desktop.
Save 623637646/3947d2f460decd0d12643f82c0424943 to your computer and use it in GitHub Desktop.
Objective C weak associated
- (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