頭が回らない問題があります。NSUserDefaults のインスタンスがあり、そこにいくつかの値を格納しました。これらの値の 1 つは、別の表示で使用されるプレーヤーのスコア (ゲーム) を含む配列です。ゲームが終了したら、配列を新しいスコアで更新し、それを NSUserDefaults に再割り当てしますが、何らかの理由で正しく動作していないようで、カウントをコンソールに出力しようとするとデバッグ中になります。 0.
問題のコードは次のとおりです。
// // SAMHighScoreInputViewController.m // Tappity // // Created by Sam on 29/05/13. // Copyright (c) 2013 Sam. All rights reserved. //
#import "SAMHighScoreInputViewController.h"
#import "SAMScoreObject.h"
@interface SAMHighScoreInputViewController ()
@end
@implementation SAMHighScoreInputViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self; }
- (void)viewDidLoad {
self.defaults = [NSUserDefaults standardUserDefaults];
self.nameScoreLabel.text = [NSString stringWithFormat:@"%@ - %@", [self.defaults objectForKey:@"lastName"], [self.defaults objectForKey:@"lastScore"]];
self.nameTextField.text = [self.defaults objectForKey:@"lastName"];
[super viewDidLoad]; // Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
- (IBAction)textFieldValueChanged:(UITextField *)sender {
self.nameScoreLabel.text = [NSString stringWithFormat:@"%@ - %@", self.nameTextField.text, [self.defaults objectForKey:@"lastScore"]];
}
- (IBAction)submitButtonTapped:(UIButton *)sender {
SAMScoreObject *score = [[SAMScoreObject alloc] init];
score.playerName = self.nameTextField.text;
score.score = [self.defaults integerForKey:@"lastScore"];
NSArray *scores = [self.defaults objectForKey:@"scoresArray"];
NSMutableArray *updatedArray = [scores mutableCopy];
[updatedArray addObject:score];
NSArray *updatedArrayScores = [updatedArray copy];
[self.defaults setObject:updatedArrayScores forKey:@"scoresArray"];
[self.defaults setObject:[self.defaults objectForKey:@"scoresArray"] forKey:@"scoresArray"];
[self.defaults synchronize];
NSLog(@"%@, %@", [self.defaults objectForKey:@"lastScore"], [self.defaults objectForKey:@"lastName"]);
NSLog(@"%i", [[self.defaults objectForKey:@"scoresArray"] count]);
[self dismissViewControllerAnimated:YES completion:nil];
} @end