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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to