• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Class Instance Storage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Class Instance Storage
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Drag from Table To View
  • Next by Date: Re: Class Instance Storage
  • Previous by thread: [SOLVED] Drag from Table To View
  • Next by thread: Re: Class Instance Storage
  • Index(es):
    • Date
    • Thread