NSMutableArray 不会添加 NSDictionary

发布于 2024-10-20 11:57:49 字数 2227 浏览 2 评论 0原文

我将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-10-27 11:57:49

您是否在 init 等中初始化 ScoreList ivar?

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

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

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];
}
羁拥 2024-10-27 11:57:49

好吧,乔治,我重新考虑了你关于稍后覆盖它的建议。

这与我的 NSUserdefaults 有关。我将它们注释掉,现在它将对象添加到我的 NSMutableArray 中。我对 NSUserdefaults 很陌生,所以我不知道如何使用它 atm 哈哈

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文