2014-03-05 9 views
0

저는 게임을 개발 중이며 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>.

어떻게해야합니까? 더 이상 세부 사항이 필요하면 알려주세요 .. 미리 감사드립니다!

답변

2

NSNumbers와 원시 정수 유형의 차이점을 이해하지 못하는 것 같습니다.

모든 데이터를 저장하는 최고 기록 (관리) 개체가있는 경우 모든 것을 NSNumber에 저장해야합니다. 하지만 (int) 형식으로 다시 캐스트 할 수는 없습니다. 거기에 도착하는 것은 해당 NSNumber 객체의 주소 일 가능성이 큽니다. 대신 [highScores.endlessSwipeHighScore integerValue]을 사용하여 논리에 사용할 적절한 기본 유형 값을 가져와야합니다.

+0

실제로 [[highScores.endlessSwipeHighScore integerValue]'는 또한'intValue'하지만 NSNumber를 반환합니다! 그것은 완벽하게 작동했습니다. 빠른 답변 주셔서 감사합니다! –

관련 문제