NSMutableArray with weak references
Here’s a category that permits to create an NSMutableArray that has weak references. This is useful when making arrays of delegates and you don’t want to retain/release them (preventing circular references).
@implementation NSMutableArray (WeakReferences) + (id)mutableArrayUsingWeakReferences { return [self mutableArrayUsingWeakReferencesWithCapacity:0]; } + (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity { CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual}; // We create a weak reference array return (id)(CFArrayCreateMutable(0, capacity, &callbacks)); } @end
This is simple but not very well documented and many people still don’t know about it. The workaround was really heavy and prone to errors. Note that the resulting NSMutableArray keeps all functionalities (fast enumeration, description, …). It just doesn’t retain/release its members.
A similar method exists already on the Mac, but is not included in iPhone for reasons I don’t really know/understand.
Thanks to mistercow for correcting me on zero versus NULL.
+[NSPointerArray pointerArrayWithWeakObjects] serves a similar purpose, though it is not an NSArray subclass.
@Sanctuary: NSPointerArray does the same thing, but is not available on the iPhone (at least in 3.0, I haven’t checked 3.1).
This example code will lead you to a world of hurt. By passing a non-zero capacity to CFArrayCreateMutable, you are creating a *fixed mutable* array. If your capacity is 5, and your array has 5 elements, and you try to insert a value, the behavior is *undefined*. Just pass zero as the capacity and avoid an entire class of difficult-to-track-down bugs that are caused by fixed mutable arrays.