MagicalRecord无法正常运行

时间:2014-03-05 11:50:24

标签: ios objective-c core-data magicalrecord

我正在开发一款游戏,我正在使用MagicalRecord存储高分。游戏有4种模式和2种“类型”,因此有8种组合可供选择,我希望每种组合都能获得高分。我正在使用这个逻辑:

NSArray *arrayOfHighscores = [Highscores MR_findAll];
if([arrayOfHighscores count] == 0) {
    Highscores *highScores = [Highscores MR_createEntity];

    highScores.normalTapHighscore = [NSNumber numberWithInt:0];
    highScores.normalSwipeHighscore = [NSNumber numberWithInt:0];
    highScores.zenTapHighscore = [NSNumber numberWithInt:0];
    highScores.zenSwipeHighscore = [NSNumber numberWithInt:0];
    highScores.crazyTapHighscore = [NSNumber numberWithInt:0];
    highScores.crazySwipeHighscore = [NSNumber numberWithInt:0];
    highScores.endlessTapHighscore = [NSNumber numberWithInt:0];
    highScores.endlessSwipeHighscore = [NSNumber numberWithInt:0];

    [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
}
Highscores *highScores = [Highscores MR_findFirst/*:[NSManagedObjectContext MR_contextForCurrentThread]*/];

if (currentGameMode == NORMAL_GAME_MODE) {
    if (currentGameType == TAP_GAME_TYPE) {
        if (score > (int)highScores.normalTapHighscore) {
            highScore = score;
            highScores.normalTapHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.normalTapHighscore;
    } else {
        if (score > (int)highScores.normalSwipeHighscore) {
            highScore = score;
            highScores.normalSwipeHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.normalSwipeHighscore;
    }
} else if (currentGameMode == ZEN_GAME_MODE) {
    if (currentGameType == TAP_GAME_TYPE) {
        if (score > (int)highScores.zenTapHighscore) {
            highScore = score;
            highScores.zenTapHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.zenTapHighscore;
    } else {
        if (score > (int)highScores.zenSwipeHighscore) {
            highScore = score;
            highScores.zenSwipeHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.zenSwipeHighscore;
    }
} else if (currentGameMode == CRAZY_GAME_MODE) {
    if (currentGameType == TAP_GAME_TYPE) {
        if (score > (int)highScores.crazyTapHighscore) {
            highScore = score;
            [highScores setCrazyTapHighscore:[NSNumber numberWithInt:score]];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.crazyTapHighscore;
    } else {
        if (score > (int)highScores.crazySwipeHighscore) {
            highScore = score;
            [highScores setCrazySwipeHighscore:[NSNumber numberWithInt:score]];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.crazySwipeHighscore;
    }
} else if (currentGameMode == ENDLESS_GAME_MODE) {
    if (currentGameType == TAP_GAME_TYPE) {
        if (score > (int)highScores.endlessTapHighscore) {
            highScore = score;
            highScores.endlessTapHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.endlessTapHighscore;
    } else {
        if (score > (int)highScores.endlessSwipeHighscore) {
            highScore = score;
            highScores.endlessSwipeHighscore = [NSNumber numberWithInt:score];
            [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait];
        } else highScore = (int)highScores.endlessSwipeHighscore;
    }
}

并且highscore变量正在屏幕上显示。同样出于测试目的,我在程序开始时调用MR_truncateAll

问题在于:我第一次玩游戏并且游戏结束时会显示游戏,我会显示当前的分数。虽然,第二次,屏幕上出现一个3位数字(即使我的分数比第一次小)。我尝试了调试,所有这些,似乎在第二次,highScores对象包含:data: <fault>

我该怎么办?如果您需要更多详细信息,请告诉我..提前致谢!

1 个答案:

答案 0 :(得分:2)

看起来你并不了解NSNumbers和原始整数类型之间的区别。

当您的高分(托管)对象保存所有数据时,您需要将所有内容存储在NSNumber中。但是你不能把它强制转换回(int)类型。你得到的可能是那个NSNumber对象的地址。相反,您应该使用 [highScores.endlessSwipeHighScore integerValue] 来获取要在逻辑中使用的正确基元类型值。