0
votes

I updated to cocos2d from 0.99.4 to 0.99.5. While it was on the older version I had the high score list working and I was able to add the NSDictionary to NSMutableArray with no problems.

Now that I've updated it won't add the NSDictionary variable scoreDetails to my NSMutableArray scoreList. Here's my code:

StatsManager.h

@interface StatsManager : NSObject {
NSMutableArray *scoreList;
NSUserDefaults *saveHighScore;
NSMutableArray *printableScoreList;

//NSMutableArray *scoreListTestOne;
float highScoreHelloWorld;
}

StatsManager.m

-(void)setHighScore:(float)highScore nameStrings:(NSString*)nameString {

NSNumber *newHighScore = [NSNumber numberWithFloat:highScore];
NSLog(@"%@ highScore", newHighScore);

NSDictionary *scoreDetails = [NSDictionary dictionaryWithObjectsAndKeys:nameString, @"name", newHighScore, @"score", nil];

NSLog(@"%@", scoreDetails);
//NSMutableArray *testTwo = [[NSMutableArray alloc] init];
[scoreList addObject:scoreDetails];
NSLog(@"scoreList %@", scoreList);
//[scoreListTestOne addObject:scoreDetails];
//NSLog(@"scoreListTestOne %@", scoreListTestOne);
//sort
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"score" ascending:NO];
[scoreList sortUsingDescriptors:[NSArray arrayWithObject:sort]];

printableScoreList = scoreList;
NSLog(@"printableScoreList %@", printableScoreList);
//NSLog(@"scoreListTestOne %@", scoreListTestOne);
}

The line in question is

    [scoreList addObject:scoreDetails];

I created a local NSMutableArray variable in the setHighScore function and tried adding the scoreDetails to that and it worked. but why doesn't it work like I've coded it above anymore?

I alloc init my scoreList here:

@implementation StatsManager
static StatsManager *_sharedStatsManager = nil;
-(id)init {
    scoreList = [[NSMutableArray alloc] init];
    //playerNames = [[NSMutableArray alloc] init];
    //playerScores = [[NSMutableArray alloc] init];
    printableScoreList = [[NSMutableArray alloc] init];

//listOfScoresTest = [[NSMutableDictionary alloc] initWithCapacity:5];
/*if ([scoreList count] == 0) {
    for (int i = 0; i < 5; i++) {
        [scoreList addObject:[NSNumber numberWithFloat:0.00]];
    }
}*/

return [super init];
}

I should also mention that I created a new projectB and transferred my files/images from my old projectA to the new one because the old one wouldn't compile anymore because of some duplicate error. But i "cleaned all targets" again and it worked but that also has the same problem as my new projectB

2
Where do you alloc init your scoreList? - Stefan H
1) Please show the code where your create the array. 2) What's the error? - Georg Schölly
I've updated the question and there's no error that stops it from running on the iphone. Just won't add the object scoreDetails to scoreList. My NSLogs for scoreDetails prints out perfect but when I do the NSLog on scoreList it returns Null. - Mark
@bigubosu: Any chance that you're overwriting it later on? What is the output of NSLog(@"testTwo %@", scoreList);? - Georg Schölly
@Georg The output for scoreDetails is { name = Dgh; score = "3.749572"; } and the output for scoreList is scoreList (null) - Mark

2 Answers

1
votes

Do you initialize scoreList ivar in init or so forth?

- (id)init
{
    /* snip */
    scoreList = [[NSMutableArray alloc] init];
    return self;
}

- (void)dealloc
{
    [scoreList release];
    [super dealloc];
}
0
votes

Ok Georg I reconsidered your suggestion about overwriting it later.

and it had something to do with my NSUserdefaults. I commented them out and now it add's the objects to my NSMutableArray. I'm pretty new to NSUserdefaults so I don't know exactly how to use it atm lol

    -(void)save
{

    //make another array to save the scores.
    saveHighScore = [NSUserDefaults standardUserDefaults]; 
    //[saveHighScore setObject:scoreListNew forKey:@"DodgerAppBeta"];
    [saveHighScore synchronize];
    //[[NSUserDefaults standardUserDefaults] setObject:scoreListTestOne forKey:@"DodgerBeta"];
    //[[NSUserDefaults standardUserDefaults] synchronize];  
}

-(void)load
{

    saveHighScore = [NSUserDefaults standardUserDefaults];
    //scoreListNew  = [[saveHighScore objectForKey:@"DodgerAppBeta"] mutableCopy];

    printableScoreList  = [[saveHighScore objectForKey:@"DodgerAppBeta"] mutableCopy];
    //NSLog(@"scoreListTestOne %@", scoreListTestOne);
    //[printableScoreList addObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"Dodger"]];
    NSLog(@"PSL %@", printableScoreList);
}