【问题标题】:NSMutableArray won't add NSDictionaryNSMutableArray 不会添加 NSDictionary
【发布时间】:2011-03-03 17:19:21
【问题描述】:

我从 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 添加到该变量中,它起作用了。但是为什么它不再像我上面编码的那样工作了?

我在这里分配初始化我的 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

【问题讨论】:

  • 你在哪里分配初始化你的 scoreList?
  • 1) 请显示您创建数组的代码。 2) 错误是什么?
  • 我已经更新了这个问题,并且没有任何错误阻止它在 iphone 上运行。只是不会将对象 scoreDetails 添加到 scoreList。我的 scoreDetails 的 NSLog 打印出来很完美,但是当我在 scoreList 上执行 NSLog 时,它返回 Null。
  • @bigubosu:你以后有没有机会覆盖它? NSLog(@"testTwo %@", scoreList); 的输出是什么?
  • @Georg scoreDetails 的输出是 { name = Dgh;分数=“3.749572”;并且 scoreList 的输出是 scoreList (null)

标签: objective-c nsmutablearray nsdictionary


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我已经像你一样初始化并发布了它
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多