Saving NSMutableArray for plist?

Possible duplicate:
iOS: save two NSMutableArray files in a .plist file

In the application I'm working on, I have a bunch of Registration objects that contain some strings, dates, and integers. All these objects are stored in NSMutableArray when they are created, and now I want to save this array in some way, so that when the user closes the application, etc., the Content can be saved and restored when the application opens again.

I read that plist is probably the best way to do this, but I cannot find examples, posts, etc. that show how to do this?

So: How can I save NSMutableArray to a plist file when the application closes, and how to restore it again?

+4
source share
1 answer

NSMutableArray has a way to do this, if you know exactly where to save it:

//Writing to file if(![array writeToFile:path atomically:NO]) { NSLog(@"Array wasn't saved properly"); }; //Reading from File NSArray *array; array = [NSArray arrayWithContentsOfFile:path]; if(!array) { array = [[NSMutableArray alloc] init]; } else { array = [[NSMutableArray alloc] initWithArray:array]; }; 

Or you can use NSUserDefaults:

 //Saving it [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"My Key"]; //Loading it NSArray *array; array = [[NSUserDefaults standardUserDefaults] objectForKey:@"My Key"]; if(!array) { array = [[NSMutableArray alloc] init]; } else { array = [[NSMutableArray alloc] initWithArray:array]; }; 
+12
source

All Articles