저는 게임을 개발 중이며 MagicalRecord를 사용하여 높은 점수를 저장하고 있습니다. 이 게임에는 4 가지 모드와 2 가지 "유형"이 있으므로 8 가지 조합을 사용할 수 있으며 각각 높은 점수를 저장하고 싶습니다.MagicalRecord가 제대로 작동하지 않습니다.
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>
.
어떻게해야합니까? 더 이상 세부 사항이 필요하면 알려주세요 .. 미리 감사드립니다!
실제로 [[highScores.endlessSwipeHighScore integerValue]'는 또한'intValue'하지만 NSNumber를 반환합니다! 그것은 완벽하게 작동했습니다. 빠른 답변 주셔서 감사합니다! –