Reputation: 395
I have a problem i cant get my head around. I have an instance of NSUserDefaults and have stored some values in there. One of these values is an array that contains the players scores (its a game) to be used in another display. After the game is finished, I update the array with the new score and reassign it to the NSUserDefaults, but for some reason it doesn't seem to be behaving correctly and in debug when i try to print the count into he console, its always 0.
Here is the code in question:
// // 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
Upvotes: 0
Views: 145
Reputation: 23001
You need to serialize your SAMScoreObject
into an NSData
object of some form before you can store it in the NSUserDefaults
store.
From the documentation:
A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
The fact that you're storing an NSArray
object isn't good enough, because the objects in that array aren't one of the supported types.
Upvotes: 2
Reputation: 19281
Where is the scoresArray
ever created? You should probably lazily create it when you attempt to update it. For example:
NSArray *scores = [self.defaults objectForKey:@"scoresArray"];
if(scores == nil)
{
scores = [NSArray array];
}
NSMutableArray *updatedArray = [scores mutableCopy];
Upvotes: 1