I want to use a static class variable to maintain state of a Playlist object which will be shared between multiple classes in my app.
I make a call to getPlaylist in my AppDelegate, so that invokes my initialize class method and sets up the MSMutableArray.
However when I invoke addItemToPlaylist, the content variable is no longer the static instance of NSMutableArray. Instead it points to a totally different address in memory- a different address each time I debug.
Am I doing anything obviously wrong? Thanks in advance.
#import "Playlist.h"
static NSMutableArray *content;
@implementation Playlist
+ (void)initialize
{
content = [NSMutableArray arrayWithCapacity:10];
}
+ (NSMutableArray *)getPlaylist
{
if ([content count] == 0)
return nil;
return content;
}
+ (void)addItemToPlaylist:(PlaylistTrack *)track;
{
[content addObject:track];
}
[NSMutableArray arrayWithCapacity]returns an auto-released object (there should be a complaint at runtime about there being no current auto-release pool).Use
[[NSMutableArray alloc] initWithCapacity]instead and add a class method to release it when done.EDIT: Cheers @Lvsti.