IOS: save two NSMutableArray files in .plist file

I want to save two NSMutableArray, which I use as a global array in AppDelegate. These two arrays are also stored with NSUserDefaults. Now I want to know how I should create this file and how I can store these two arrays every time I modify them. Can you help me?

+8
ios objective-c xcode nsmutablearray plist
source share
1 answer
  • Create an NSArray containing 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 an array from a file.

     NSString *path = [bundle pathForResource:@"file" ofType:@"plist"]; NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil); 
+12
source share

All Articles