iOS: store two NSMutableArray in a .plist file
Asked Answered
P

1

8

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?

Payload answered 20/5, 2011 at 10:26 Comment(0)
N
12
  1. Create an NSArray containing your two NSMutableArrays.

    NSArray *array = [NSArray arrayWithObjects:<#(id), ...#>, nil];
    
  2. 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];
    
  3. Load the array from the file.

    NSString *path = [bundle pathForResource:@"file" ofType:@"plist"];
    NSArry *array = (path != nil ? [NSArray arrayWithContentsOfFile:location] : nil);
    
Narra answered 20/5, 2011 at 11:11 Comment(3)
and when I save it again? I want replace it, how can I do?Payload
Have you checked that your NSArrays aren't nil?Narra
How to extract the NSMutableArray from the array (NSArray in the code)?Stroud

© 2022 - 2024 — McMap. All rights reserved.