views:

931

answers:

3
+1  Q: 

Saving a NSArray.

I would like to save an NSArray either as a file or possibly use user defaults. Here's what I am hoping to do.

  1. Retrieve already saved NSArray (if any).
  2. Do something with it.
  3. Erase saved data (if any).
  4. Save the NSArray.

Is this possible, and if so how should I do this?

+1  A: 

This would seem to be a problem most suited to Core Data as this will deal with all the persistent object data. When you retrieve you data it will return an NSSet, which is unsorted so you will have to have some way of sorting the data in the array such as a unique id number assocaited with each object you create.

Ian Turner
+2  A: 

NSArray provides you with two methods to do exactly what you want: initWithContentsOfFile: and writeToFile:atomically:

A short example might look like this:

//Creating a file path under iPhone OS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];

//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
    //Array file didn't exist... create a new one
    yourArray = [[NSMutableArray alloc] initWithCapacity:10];

    //Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];
racha
Thanks! One question will `writeToFile:` create a new file with the array in at the Specified Path?
Joshua
Yes. One caveat: The array must be a valid property list, which means that all its contents (and their contents, where appropriate) must also be property lists.
Peter Hosey
I see, that should be ok. So, when I load the Array with `initWithContentsOfFile:` do I have to give the path directly to the file, and if so what will it be called?
Joshua
I've updated the code to show you how you create the file name and path.
racha
Joshua: The documentation for `initWithContentsOfFile:` tells you what you need to give it. As for the name of the variable containing the pathname: It's whatever you named it.
Peter Hosey
+3  A: 

You could implement NSCoding on the objects the array contains and use NSKeyedArchiver to serialize/deserialize your array to disk.

BOOL result = [NSKeyedArchiver archiveRootObject:myArray toFile:path];

The archiver will defer to your NSCoding implementation to get serializable values from each object and write a file that can be read with NSKeyedUnarchiver:

id myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

More info in the serialization guide.

dbarker
Yes. I wanted to add that this way must be used if the array contains non-PropertyList friendly data (see Racha's answer for PropertyList friendly data).
Matt Gallagher