Weak object in an NSDictionary? Weak object in an NSDictionary? ios ios

Weak object in an NSDictionary?


In iOS 6+ you can use NSMapTable and choose if you want objects and/or keys to be weak or strong.


I've settled on defining a "container" class with a single member, like this:

@interface Parent : NSObject@property (nonatomic, weak) id parent;@end@implementation Parent@synthesize parent = _parent;- (id)initWithParent: (id)parent;{    if (( self = [super init] )) {        _parent = parent;    }    return self;}@end

And using it:

id parentRef = [[Parent alloc] initWithParent: parent];[newElement setObject: parentRef forKey: ParentKey];

I think this will work for me, but it seems crazy that there's no better way built in to Foundation.


There is a built-in way; just use:

[array addObject:[NSValue valueWithNonretainedObject:object]];

Then to access the object later, use:

id object = [[array objectAtIndex:i] nonretainedObjectValue];

If the value has been released since you added it then "object" will be an invalid pointer under iOS 4.x, and (I'm assuming) will be nil under 5.x with ARC enabled.

EDIT: my assumption was wrong - it's not weak under ARC (see discussion below).