I have a class, let's say Chicken
, and I want a class-level method to enumerate all the currently existing Chicken
s. To do this, I keep a class-level NSMutableArray
and add self
to this in the init
method.
This is great and my enumeration method simply returns a (non-mutable) pointer to this array.
The problem is that I can no longer deallocate a chicken by removing all pointers to it, as there is always a strong pointer left in the array.
E.g. If I do this...
Chicken *chick = [[Chicken alloc] init];
// Do something with the chick
chick = nil;
The chicken lives on because there is a strong pointer to it in the array. I could have a -[Chicken kill]
method, which removes it from the array, but that's not neat.
What's the neatest way around this?