开发者

NSMutableArray won't add NSDictionary

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 *saveHighSco开发者_如何学运维re;
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


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];
}


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜