gpt4 book ai didi

ios - MagicalRecord 无法正常运行

转载 作者:行者123 更新时间:2023-11-28 22:14:04 32 4
gpt4 key购买 nike

我正在开发一款游戏,我正在使用 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在程序的开头。

这是问题所在:我第一次玩游戏时,游戏结束时显示游戏结束 View ,显示的是我当前的分数。虽然,第二次,屏幕上出现了一个 3 位数(尽管我的分数低于第一次)。我试过调试等等,似乎是第二次,highScores对象包含:data: <fault> .

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

最佳答案

看来您没有理解 NSNumbers 和原始整数类型之间的区别。

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

关于ios - MagicalRecord 无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22196912/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com