NSArray of weak references (__unsafe_unretained) to objects under ARC

As Jason said, you can’t make NSArray store weak references. The easiest way to implement Emile’s suggestion of wrapping an object inside another object that stores a weak reference to it is the following:

NSValue *value = [NSValue valueWithNonretainedObject:myObj];
[array addObject:value];

Another option: a category that makes NSMutableArray optionally store weak references.

Note that these are “unsafe unretained” references, not self-zeroing weak references. If the array is still around after the objects are deallocated, you’ll have a bunch of junk pointers.

Leave a Comment