views:

143

answers:

2

Alright, here goes. I'm making a very basic app, and I want the user to be able to store information between sessions. Basically, for them to save their game. There are only 7 Variables I need to save, all of them in are Integers.

This is the code I have to save the game.

- (NSString *)dataFilePath
{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 return [documentsDirectory stringByAppendingPathComponent:fileName];
}

- (IBAction)saveGameAction:(id)sender
{
 NSString *test1 = [[NSString alloc] initWithFormat:@"%d",varMoney];


 NSMutableArray *array = [[NSMutableArray alloc] init];
 [array addObject:test1];
 [array writeToFile:[self dataFilePath] atomically:YES];
 [array release];


 [test1 release];

}

That works (For simplicity Sake, I am only storing 1 of the Variables, not all 7 in this example code. What doesn't work is retrieving the values from the Property List. Upon hitting the "Load" button, I want to take the Variable out of the List and put it back into the correct Variable. This is the code I have for that:

- (IBAction)loadGameAction:(id)sender
{
 NSString *filePath = [self dataFilePath];
 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
 {


  NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
  NSString *testing = [array objectAtIndex:0];
  [array release];

  varMoney = [testing intValue];




  NSString *testMessage = [[NSString alloc] initWithFormat:@"%d", varMoney];
  //NSString *testMessage = [[NSString alloc] initWithFormat:@"%@", testing];

  UIAlertView *alert = [[UIAlertView alloc]
         initWithTitle:@"Test"
         message:testMessage
         delegate:nil cancelButtonTitle:@"Cool"
         otherButtonTitles:nil];
  [alert show]; 
  [alert release];



 }

Whenever I load the code, the program crashes. Ideally, I would like to store the Int's directly in the Property List, instead of NSStrings, and then retrieve the Int's from the Property list. I tried fooling around with NSNumber but could not for the life of me get it to work.

So that's my problem. I can't read the values from the Property List. I'd like your help to get this issue fixed, thanks!

+3  A: 

Instead of messing around with this for only 7 values, check out NSUserDefaults. Very simple to implement and reliable.

Saving:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:42 forKey:@"integerKey"];
[prefs synchronize];

Retrieving:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [prefs integerForKey:@"integerKey"];
coneybeare
Alright, lemme try this out and I'll see how it works. Thanks for the fast response!
Wayfarer
It worked! I used UserDefaults to save all the settings and it was soooo much easier. Thank you so much!
Wayfarer
+1  A: 

U dont need to do user defaults

in your code just change one line and it works:

NSString *testing = [[array objectAtIndex:0] retain];

u release the array, and your string gets autoreleased

ahmet emrah