내가 만들고있는 게임에는 플레이어가 잃을 때 할당되는 highScore 정수 변수가 있습니다. 내 높은 점수를 저장 NSUsersDefaults 클래스를 사용하고 있습니다. 다음은 내가 사용하는 코드입니다.매우 간단하게 저장된 최고 점수
-(void)saveScore {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"highScore"];
[defaults setInteger:score forKey:@"highScore"];
[defaults synchronize];
NSLog(@"High Score: %i ", highScore);
}
-(IBAction)buttonReleased:(id)sender {
[stopWatchTimer invalidate];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *label0 = @"Hold to Start";
[labelText setText:label0];
if (score > 0) {
score--;
}
else {
score = 0;
NSLog(@"Changed score to 0");
}
if (score > highScore) {
[self saveScore];
NSString *scoreMessage =[[NSString alloc] initWithFormat:@"Congrats! You have a new High Score! Click Share High Score to share your score of: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"High Score!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
else {
NSString *scoreMessage =[[NSString alloc] initWithFormat:@"Game Over! Your score was: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"GAME OVER!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:@"Try Again" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
- (void)viewDidLoad
{
[super viewDidLoad];
int highscore = [[NSUserDefaults standardUserDefaults] integerForKey: @"highScore"];
[stopWatchTimer invalidate];
stopWatchTimer=nil;
}
나는이 시간 동안 씨름하고 있습니다! 내가 도대체 뭘 잘못하고있는 겁니까?! 참고 : 가능한 한 간단하게 설명 할 수 있습니까?
감사합니다. - 매트
'NSUserDefaults'를 사용하는 것이 가장 간단한 방법이지만, '동기화'메시지를 보내지 않으면 데이터가 저장되지 않습니다. – Hyperbole
이 NSUserDefaults 동기화 메서드는 어디에서 추가 할 수 있습니까? 그리고 그것은 어떻게 생겼습니까? – 64bitman
'synchronize'는 데이터를 영속성을 보장하기 위해 기본 사전에 추가 한 후에 호출해야합니다. 명확한 설명을 원하면 [the docs] (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html)를 확인하십시오. – Hyperbole