How can I get an array of zeroing weak references under ARC? I don\'t want the array to retain the objects. And I\'d like the array elements either to remov
If you are working with at least MacOS X 10.5, or iOS6, then:
Note that the collections may not immediately notice that objects have gone away, so counts could still be higher, and keys could still exist even though the associated object is gone, etc. NSPointerArray has a -compact method which should in theory get rid of any nilled pointers. NSMapTable docs note that the keys for weakToStrong maps will remain in the table (even though effectively nil) until it is resized, meaning the strong object pointers can remain in memory even if no longer logically referenced.
Edit: I see the original poster asked about ARC. I think it was indeed 10.8 and iOS 6 before those containers could be used with ARC -- the previous "weak" stuff was for GC, I think. ARC wasn't supported until 10.7, so it's really a question if you need to support that release and not 10.6, in which case you would need to roll your own (or perhaps use custom functions with NSPointerFunctions, which can then in turn be used with NSPointerArray, NSHashTable, and NSMapTable).
I just create non-threadsafe weak ref version of NSMutableDictionary and NSMutableSet. Code here: https://gist.github.com/4492283
For NSMutableArray, things is more complicated because it cannot contain nil
and an object may be added to the array multiple times. But it is feasible to implemented one.