How to Use a Nested Array to Make Your Complex Data Persistent

iStock_000006265667XSmall.jpgSometimes you are working with data that is more complicated than a simply list of items. Wouldn’t it be nice to be able to make that data persistent without needing a separate file for each object in your project?

Saving Data with Arrays

Last week, I showed you how you can use NSMutableArray to save data to the iPhone’s filesystem and then retrieve the contents the next time the app is used. It turns out that you can nest arrays to build more complicated data objects and the system is smart enough to save the data these arrays in the same way.

Example

Let us think about having three arrays: savedArray, array1 and array2. savedArray is going to act as a container for array1 and array2. We are going to put data into array1 and array2 and then add these two arrays to savedArray.

//create an array and add values to it:
NSMutableArray *savedArray = [[NSMutableArray alloc] init];
NSMutableArray *array1 = [[NSMutableArray alloc] init];
NSMutableArray *array2 = [[NSMutableArray alloc] init];
[array1 addObject:@"array1-One"];
[array1 addObject:@"array1-Two"];
[array1 addObject:@"array1-Three"];
[savedArray addObject:array1];
[array2 addObject:@"array2-One"];
[array2 addObject:@"array2-Two"];
[array2 addObject:@"array2-Three"];
[savedArray addObject:array2];

In effect, this creates a two-way table of data (like a spreadsheet) that you can visualize like this:

array2

array2-One

array2-Two

array2-Three

array1
array1-One array1-Two array1-Three

 

As you can see this is a richer data structure than a simple list. But, you can save it in the same way that you did last week:

//this statement is what actually writes out the array
//to the file system:
[savedArray writeToFile:[self arraySaveFileName] atomically:NO];

When you need to retrieve the data you simply use the same method as we discussed last week. But, you need to keep in mind that you are working with an array of arrays. So, you will need to tweek the retrieval process a bit:

//retrieve your array by using initWithContentsOfFile while passing
//the name of the file where you saved the array contents.
NSMutableArray *savedArray = [[NSMutableArray alloc] initWithContentsOfFile:@"UseYourFileNameHere"];
NSMutableArray *arrayWithData = [savedArray objectAtIndex:0];
NSString *yourData = [array objectAtIndex:0];

You can think of savedArray above as the column in the spreadsheet and arrayWithData as the row. Like when you are working with a spreadsheet you look up the value that want by first selecting the column using objectAtIndex on savedArray and then retrieving the value from the row by using objectAtIndex on arrayWithData.

This excites me because it is a simple way to make data persistent without using a SQLite database. You can use your imagination when it comes to how you would implement this in your own object hierarchies. The other nice thing is that you only need to manage one file (as opposed to requiring a save file for each object in your app.

So what do you think? Do you have any tips on how to achieve data persistence on the iPhone?

One Response to How to Use a Nested Array to Make Your Complex Data Persistent

  1. iPhoneKicks.com April 24, 2009 at 7:19 pm #

    How to Use a Nested Array to Make Your Complex Data Persistent…

    You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

Leave a Reply

Switch to our mobile site