I want to store two NSMutableArray that I use as global array in AppDelegate. These two array are also store with NSUserDefaults. Now I want to know how I must create this file and how can I store these two array everytime I modify them. Can You help me?
Asked
Active
Viewed 5,555 times
1 Answers
12
Create an NSArray containing your two NSMutableArrays.
NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil];
Write the array to a file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryDirectory = [paths objectAtIndex:0]; NSString *location = [libraryDirectory stringByAppendingString:@"/somefilename.plist"]; [array writeToFile:location atomically:YES];
Load the array from the file.
NSString *path = [bundle pathForResource:@"file" ofType:@"plist"]; NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil);

Julian
- 1,573
- 2
- 22
- 36
-
and when I save it again? I want replace it, how can I do? – cyclingIsBetter May 20 '11 at 13:12
-
Have you checked that your NSArrays aren't nil? – Julian May 20 '11 at 19:10
-
How to extract the NSMutableArray from the array (NSArray in the code)? – Sankar Chandra Bose Sep 10 '11 at 12:50