我从0.99.4更新到cocos2d到0.99.5。虽然它是在旧版本上我有高分列表工作,我能够将NSDictionary添加到NSMutableArray没有问题。
现在我已更新它,不会将NSDictionary变量scoreDetails添加到我的NSMutableArray scoreList。这是我的代码:
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);
}
有问题的行是
[scoreList addObject:scoreDetails];
我在setHighScore函数中创建了一个本地NSMutableArray变量,并尝试将scoreDetails添加到该变量并且它有效。但为什么它不能像我上面编码一样工作呢?
我在这里分配init my scoreList:
@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];
}
我还应该提一下,我创建了一个新的projectB并将我的文件/图像从旧的projectA转移到了新的projectA,因为旧版本因为一些重复的错误而不再编译。但我再次“清理了所有目标”并且它有效,但这也与我的新项目B有相同的问题
答案 0 :(得分:1)
你是否在init中初始化scoreList ivar?
- (id)init
{
/* snip */
scoreList = [[NSMutableArray alloc] init];
return self;
}
- (void)dealloc
{
[scoreList release];
[super dealloc];
}
答案 1 :(得分:0)
Ok Georg我重新考虑了你以后有关覆盖它的建议。
它与我的NSUserdefaults有关。我对它们进行了评论,现在它将对象添加到我的NSMutableArray中。我对NSUserdefaults很新,所以我不确切知道如何使用它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);
}