Storing custom objects to disk on the iPhone

In this tutorial I will show you how you can store custom objects to disk on the iPhone. I will create a simple array containing custom Person objects. Then I will store the array to disk and afterwards load the array. The clue is to implement the NSCoding protocol in the Person class, or the classes of the objects that we have in our array or dictionary. It’s quite simple actually.

Person.h

In the code below we define the interface of the Person class. Notice the NSCoding protocol in the interface header.

@interface Person : NSObject <NSCoding> { NSString *firstName; NSString *secondName; NSString *phone; } @property (nonatomic, retain)NSString *firstName; @property (nonatomic, retain)NSString *lastName; @property (nonatomic, retain)NSString *phone;

Person.m

In the code below we can see the person implementation. Notice the implementation of the NSCoding protocol. We have to implement two methods, encodeWithCoder and initWithCoder. We tell the NSCoding protocol that we want to decode and encode all of the person’s members. We give the objects keys in the first method so that we are able to find them again later when we decode.

@implementation Person @synthesize firstName, secondName, phone; - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:firstName forKey:@"firstName"]; [encoder encodeObject:lastName forKey:@"lastName"]; [encoder encodeObject:phone forKey:@"phone"]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.firstName = [decoder decodeObjectForKey:@"firstName"]; self.lastName = [decoder decodeObjectForKey:@"lastName"]; self.phone = [decoder decodeObjectForKey:@"phone"]; } return self; }

Saving and loading an array

In the code below we populate an array with two person objects, then we save the array to disk and afterwards load it.

// Create two person objects Person *person1 = [[Person alloc] init]; person1.firstName = @"Peter"; person1.lastName = @"Peterson"; person1.phone = @"12345678"; Person *person2 = [[Person alloc] init]; person2.firstName = @"Andrea"; person2.lastName = @"Peterson"; person2.phone = @"12345687"; // Put them into an array NSArray *array = [[NSArray alloc] initWithObjects:person1, person2, nil]; // Save the array NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; NSString *fullFileName = [NSString stringWithFormat:@"%@/ourArray", docDir]; [NSKeyedArchiver archiveRootObject:array toFile:fullFileName]; // Load the array NSMutableArray *arrayFromDisk = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileName];

Conclusion

Follow this tutorial if you want to save your arrays or dictionaries to disk for later use. Following this method is very simple and it actually works. I’ve read alot of different tutorials on the web, most of them suggest storing your objects either as .plist files or by using SQLite. I think following this method is both easy and efficient. Read more about the NSCoding protocol here.

EDIT: There was a bug in my code. fullFileName was missing %@/ in the stringWithFormat message.

Leave a Reply