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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Comments (4)

PoltrasSeptember 19th, 2009 at 12:17 pm

Thanks to mistercow for correcting me on zero versus NULL.

SanctuarySeptember 19th, 2009 at 1:22 pm

+[NSPointerArray pointerArrayWithWeakObjects] serves a similar purpose, though it is not an NSArray subclass.

PoltrasSeptember 19th, 2009 at 1:56 pm

@Sanctuary: NSPointerArray does the same thing, but is not available on the iPhone (at least in 3.0, I haven’t checked 3.1).

codewarriorSeptember 19th, 2009 at 2:17 pm

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.

Leave a comment

Your comment