<?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>Gierad Blogs &#187; Code Snippets</title>
	<atom:link href="http://www.gierad.com/blog/category/engineering/code-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gierad.com/blog</link>
	<description>because he dreams for the wisdom of an owl, but has a memory of a goldfish</description>
	<lastBuildDate>Sun, 08 Aug 2010 23:38:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>iPhone SDK: Dealing with .plist Files</title>
		<link>http://www.gierad.com/blog/2009/11/28/iphone-sdk-dealing-with-plist-files/</link>
		<comments>http://www.gierad.com/blog/2009/11/28/iphone-sdk-dealing-with-plist-files/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 15:25:13 +0000</pubDate>
		<dc:creator>gierad</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Engineering]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://www.gierad.com/blog/?p=27</guid>
		<description><![CDATA[Loading Contents of .plist file to an Array The most straightforward method for dealing with .plist files is +[NSArray initWithContentsOfFile:path]. However, you need to specify the correct path of the plist file via [NSBundle pathForResource:plist ofType:]. Loading to a Dictionary To load the contents of a .plist file to a dictionary,  instantiate a NSDictionary or [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Loading Contents of .plist file to an Array</strong></p>
<p>The most straightforward method for dealing with .plist files is +[NSArray initWithContentsOfFile:path]. However, you need to specify the correct path of the plist file via [NSBundle pathForResource:plist ofType:].</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="objc" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>arrayFromPlist<span class="sy0">:</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>plist <span class="br0">&#123;</span>
     <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>path <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span class="kw5">NSBundle</span></a> mainBundle<span class="br0">&#93;</span> pathForResource<span class="sy0">:</span>plist ofType<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;plist&quot;</span><span class="br0">&#93;</span>;
     <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> <span class="sy0">*</span>array <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span class="kw5">NSArray</span></a> alloc<span class="br0">&#93;</span> initWithContentsOfFile<span class="sy0">:</span>path<span class="br0">&#93;</span>;
     <span class="kw1">return</span> array;
 <span class="br0">&#125;</span></pre></div></div>
<p><strong>Loading to a Dictionary</strong></p>
<p>To load the contents of a .plist file to a dictionary,  instantiate a NSDictionary or NSMutableDictionary and initialize it with [NSDictionary initWithContentsOfFile:]</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="objc" style="font-family:monospace;"><span class="sy0">-</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>dictionaryFromPlist<span class="sy0">:</span> <span class="br0">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span><span class="br0">&#41;</span>plist <span class="br0">&#123;</span>
 <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>path <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/"><span class="kw5">NSBundle</span></a> mainBundle<span class="br0">&#93;</span> pathForResource<span class="sy0">:</span>plist ofType<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;plist&quot;</span><span class="br0">&#93;</span>;
 <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span>dict <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> alloc<span class="br0">&#93;</span> initWithContentsOfFile<span class="sy0">:</span>path<span class="br0">&#93;</span>;
 <span class="kw1">return</span> dict;
<span class="br0">&#125;</span></pre></div></div></p>
<p><strong>Writing to a .plist</strong></p>
<p>The easiest way to write values to a .plist file is to use a NSDictionary and modify its contents using -[NSDictionary setObject: forKey:]. To save the contents to a .plist file, use -[NSDictionary writeToFile: atomically:].</p>
<p>Atomically writing to a file guarantees that the file, if it exists at all, does not become corrupted even when the system should crash while writing.</p>
<p style="font: normal normal normal 10px/normal Monaco; margin: 0px;"><div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.gierad.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span class="kw5">NSString</span></a> <span class="sy0">*</span>path <span class="sy0">=</span> <span class="co3">@</span><span class="st0">&quot;Path to Plist.plist&quot;</span>;
 <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> <span class="sy0">*</span>dict <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span class="kw5">NSDictionary</span></a> alloc<span class="br0">&#93;</span> initWithContentsOfFile<span class="sy0">:</span>path<span class="br0">&#93;</span>;
<span class="br0">&#91;</span>dict setObject<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;Gierad&quot;</span> forKey<span class="sy0">:</span><span class="co3">@</span><span class="st0">&quot;name&quot;</span><span class="br0">&#93;</span>;
<span class="br0">&#91;</span>dict writeToFile<span class="sy0">:</span>path atomically<span class="sy0">:</span><span class="kw2">YES</span><span class="br0">&#93;</span>;</pre></div></div></p>
<p>Lastly, if you want to read/write small configurations for your app, <em>NSUserDefaults</em> is recommended. It contains methods to easily handle key-value pairs including floats and other objects.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">
]]></content:encoded>
			<wfw:commentRss>http://www.gierad.com/blog/2009/11/28/iphone-sdk-dealing-with-plist-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
