Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Revert "Removing weaklyAssociateValue because ASSIGN doesn't actually…

… make a weak association."

This reverts commit 0b41c2d. It turns out I lied about ASSIGN not making a weak association. I just tested it wrong.
  • Loading branch information...
commit 687fc77ed1874bee6db5418e09a0e432a6da3fc2 1 parent 0b41c2d
Andy Matuschak authored
3  NSObject+AssociatedObjects.h
@@ -8,6 +8,7 @@
8 8 #import <Cocoa/Cocoa.h>
9 9
10 10 @interface NSObject (AMAssociatedObjects)
11   -- (void)associateValue:(id)value withKey:(void *)key; // Retains value.
  11 +- (void)associateValue:(id)value withKey:(void *)key; // Strong reference
  12 +- (void)weaklyAssociateValue:(id)value withKey:(void *)key;
12 13 - (id)associatedValueForKey:(void *)key;
13 14 @end
5 NSObject+AssociatedObjects.m
@@ -15,6 +15,11 @@ - (void)associateValue:(id)value withKey:(void *)key
15 15 objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_RETAIN);
16 16 }
17 17
  18 +- (void)weaklyAssociateValue:(id)value withKey:(void *)key
  19 +{
  20 + objc_setAssociatedObject(self, key, value, OBJC_ASSOCIATION_ASSIGN);
  21 +}
  22 +
18 23 - (id)associatedValueForKey:(void *)key
19 24 {
20 25 return objc_getAssociatedObject(self, key);

2 comments on commit 687fc77

Kai Wu
kaiwu commented on 687fc77

Would you kindly explain how to correctly test the weak association ?
i am having the same concern, as it seems the associated value (using assign) is still valid after the object it references has been released

Kai Wu
kaiwu commented on 687fc77

seems it cannot be tested in iOS, which does not support GC and the zeroing week reference behavior

Please sign in to comment.
Something went wrong with that request. Please try again.