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!