Class Instance Storage
Class Instance Storage
- Subject: Class Instance Storage
- From: Richard Somers <email@hidden>
- Date: Sat, 25 Sep 2010 16:31:50 -0600
I need a class, in which an instance of the class, can access all
other instances of the class.
In the following code, instances of the class are stored in a static
mutable array. To make the code work, the instance must released after
adding it to the array and retained before removing it from the array.
Otherwise dealloc will never be called, and instances will never be
removed from the array.
// MyClass.m
static NSMutableArray *_array;
@implementation MyClass
+ (void)initialize
{
_array = [[NSMutableArray array] retain]; // Is retain correct
here?
}
- (id)init
{
self = [super init];
if (self) {
[_array addObject:self];
[self release]; // ???
}
return self;
}
- (void)dealloc
{
[self retain]; // ???
[_array removeObject:self];
[super dealloc];
}
@end
This code seems to work fine. Is there a better way to do this?
--Richard
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden