Last active
January 1, 2016 05:39
-
-
Save dodikk/8100423 to your computer and use it in GitHub Desktop.
Object Association misuse
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
static char ownershipsKey; // one for all objects | |
@implementation NSObject (Ownerships) | |
//should not autorelease returned value | |
- (NSMutableArray *)lazyOwnerships | |
{ | |
NSMutableArray *result = objc_getAssociatedObject(self, &ownershipsKey); | |
if (!result) { | |
result = [NSMutableArray new]; | |
// array instance per object | |
objc_setAssociatedObject(self, | |
&ownershipsKey, | |
result, | |
OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment