<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ScratchMyTail.com &#187; NSCoding</title>
	<atom:link href="http://www.scratchmytail.com/tag/nscoding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.scratchmytail.com</link>
	<description>My little tech blog</description>
	<lastBuildDate>Tue, 17 Jan 2012 21:21:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Storing custom objects to disk on the iPhone</title>
		<link>http://www.scratchmytail.com/2009/11/06/storing-custom-objects-to-disk-on-the-iphone/</link>
		<comments>http://www.scratchmytail.com/2009/11/06/storing-custom-objects-to-disk-on-the-iphone/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 13:18:13 +0000</pubDate>
		<dc:creator>ScratchMyTail</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[load array]]></category>
		<category><![CDATA[NSCoder]]></category>
		<category><![CDATA[NSCoding]]></category>
		<category><![CDATA[save array]]></category>
		<category><![CDATA[serialize objects]]></category>

		<guid isPermaLink="false">http://www.scratchmytail.com/?p=181</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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&#8217;s quite simple actually.</p>
<p><span id="more-181"></span></p>
<h3>Person.h</h3>
<p>In the code below we define the interface of the Person class. Notice the NSCoding protocol in the interface header.</p>
<blockquote><p>@interface Person : NSObject &lt;NSCoding&gt; { NSString *firstName; NSString *secondName; NSString *phone; } @property (nonatomic, retain)NSString *firstName; @property (nonatomic, retain)NSString *lastName; @property (nonatomic, retain)NSString *phone;</p></blockquote>
<h3>Person.m</h3>
<p>In the code below we can see the person implementation. Notice the implementation of the NSCoding protocol. We have to implement two methods, <em>encodeWithCoder</em> and <em>initWithCoder</em>. We tell the NSCoding protocol that we want to decode and encode all of the person&#8217;s members. We give the objects keys in the first method so that we are able to find them again later when we decode.</p>
<blockquote><p>@implementation Person @synthesize firstName, secondName, phone; &#8211; (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:firstName forKey:@"firstName"]; [encoder encodeObject:lastName forKey:@"lastName"]; [encoder encodeObject:phone forKey:@"phone"]; } &#8211; (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; }</p></blockquote>
<h3>Saving and loading an array</h3>
<p>In the code below we populate an array with two person objects, then we save the array to disk and afterwards load it.</p>
<blockquote><p>// Create two person objects Person *person1 = [[Person alloc] init]; person1.firstName = @&#8221;Peter&#8221;; person1.lastName = @&#8221;Peterson&#8221;; person1.phone = @&#8221;12345678&#8243;; Person *person2 = [[Person alloc] init]; person2.firstName = @&#8221;Andrea&#8221;; person2.lastName = @&#8221;Peterson&#8221;; person2.phone = @&#8221;12345687&#8243;; // 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];</p></blockquote>
<h3>Conclusion</h3>
<p>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&#8217;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 <a title="NSCoding" href="http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html">here</a>.</p>
<p>EDIT: There was a bug in my code. fullFileName was missing %@/ in the stringWithFormat message.</p>
<p>EDIT 2: For storing a lot of data I will recommend CoreData. Store and retrieve regular objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.scratchmytail.com/2009/11/06/storing-custom-objects-to-disk-on-the-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.scratchmytail.com/wp-content/plugins/mystat/mystat.php?act=time_load&id=87238&rnd=615907648" /></channel>
</rss>

