Note that there are some explanatory texts on larger screens.

plurals
  1. POProperty Lists - Reading and Writing (iPhone)
    text
    copied!<p>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.</p> <p>This is the code I have to save the game.</p> <pre><code>- (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]; } </code></pre> <p>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:</p> <pre><code>- (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]; } </code></pre> <p>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.</p> <p>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!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload