spaderdabomb
spaderdabomb

Reputation: 942

Trying to pass NSMutableArray from singleton

I've created a permanent data collecting class named DataHolder as a singleton. I want to store all of my highscores from each levels in an array, and then have access to that array in another class. What I've attempted to do I have laid out in the code below. When I log the value of the array, it seems to randomly log some property of it, for example CCColor: 0x175bf270. If I ever try to equate anything to it, or some of its values contained in the array, then the next time I run it, it will error before I even get past the log, just saying Bad Access (code=1, address 0x10. Any suggestions on how to avoid this while doing the same thing? Thanks!

DataHolder.h

#import <Foundation/Foundation.h>

@interface DataHolder : NSObject

+ (DataHolder *)sharedInstance;

@property (nonatomic, assign) NSMutableArray* levelHighscoresArray;

@end

DataHolder.m

@implementation DataHolder

- (id) init
{
    self = [super init];
    if (self)
    {
        self.levelHighscoresArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithInt:10], nil];
        NSLog(@"highscores array level 1 %@", _levelHighscoresArray[0]); //works fine
    }
    return self;
}

+ (DataHolder *)sharedInstance
{
    static DataHolder *_sharedInstance = nil;
    static dispatch_once_t onceSecurePredicate;
    dispatch_once(&onceSecurePredicate,^
                  {
                      _sharedInstance = [[self alloc] init];
                  });

    return _sharedInstance;
}

OtherClassImAccessingArrayFrom.m

-(void)viewDidLoad{
    [super viewDidLoad];    
    NSLog(@"temphighscores 1 %@", [[DataHolder sharedInstance] levelHighscoresArray]);
    //The above gives an error Thread 1:EXC_BAD_ACCESS (code=1, address=0x10)
}

Upvotes: 0

Views: 48

Answers (1)

Hashmat Khalil
Hashmat Khalil

Reputation: 1816

it looks good except for this part.

@property (nonatomic, assign) NSMutableArray* levelHighscoresArray;

i would change it to

@property (nonatomic, strong) NSMutableArray* levelHighscoresArray;

Upvotes: 2

Related Questions