NSMutableArray 不会添加 NSDictionary
我将cocos2d从0.99.4更新到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 我的 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];
}
我还应该提到,我创建了一个新项目 B 并将我的文件/图像从旧项目 A 转移到新项目,因为旧项目由于一些重复错误而无法再编译。但我再次“清理了所有目标”并且它起作用了,但这也与我的新项目B有同样的问题
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在 init 等中初始化 ScoreList ivar?
Do you initialize scoreList ivar in init or so forth?
好吧,乔治,我重新考虑了你关于稍后覆盖它的建议。
这与我的 NSUserdefaults 有关。我将它们注释掉,现在它将对象添加到我的 NSMutableArray 中。我对 NSUserdefaults 很陌生,所以我不知道如何使用它 atm 哈哈
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