<?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>[iPhoneiOS dev:tips]; &#187; Data / File Management</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/data-file-management/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:59:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Get Total and Free Space on the Mounted File System</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html#comments</comments>
		<pubDate>Mon, 14 Jun 2010 12:27:09 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6329</guid>
		<description><![CDATA[I recently was writing code to manage the download and storing of mp4 files to enable an application to play movies off-line. One consideration when dealing with files of significant size (50-100 megs) is disk space availability &#8211; this tip shows the method I called to get information about the mounted file system. Get Free [...]]]></description>
			<content:encoded><![CDATA[<p>I recently was writing code to manage the download and storing of mp4 files to enable an application to play movies off-line. One consideration when dealing with files of significant size (50-100 megs) is disk space availability &#8211; this tip shows the method I called to get information about the mounted file system.</p>
<h5>Get Free Disk Space</h5>
<p>To get free space available, the class <strong>NSFileManager</strong> provides a method <strong>attributesOfFileSystemForPath:error:</strong> which will return a dictionary that includes five entries with various file system information.<br />
<span id="more-6329"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get access to a file manager as our means</span>
<span style="color: #11740a; font-style: italic;">// to perform file operations</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Using the application home directory, get dictionary of attributes</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>attributesDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager attributesOfFileSystemForPath<span style="color: #002200;">:</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Print total file system size and available space  </span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File system size: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;File system free space: %lld&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>attributesDict objectForKey<span style="color: #002200;">:</span>NSFileSystemFreeSize<span style="color: #002200;">&#93;</span> longLongValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Notice that the objects for the keys requested are <strong>NSNumbers</strong>. Calling the method <strong>longLongValue</strong> returns the value of the object as a long long type, which I use to print information to console.</p>
<p>For information on the additional attributes in the dictionary, check out the NSFileManager API documenation.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-total-and-free-space-on-the-mounted-file-system.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Read Files from the Application Bundle</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html#comments</comments>
		<pubDate>Wed, 31 Mar 2010 08:09:30 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6059</guid>
		<description><![CDATA[Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing. As an example of the later, often times I find it [...]]]></description>
			<content:encoded><![CDATA[<p>Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing.</p>
<p>As an example of the later, often times I find it helpful to put json (or xml) content into a file that I know is valid based on a remote server that I will eventually connect with. I add this file to the application bundle and read it into an internal data structure. I can then do various tests with known data, and once that is working, I can then proceed to the networking side of things to connect and retrieve live data.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Read in and store as string</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;jsonTest&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;txt&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithContentsOfFile<span style="color: #002200;">:</span>file 
   encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding error<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
debug<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;str: %@&quot;</span>, str<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Do something with the test data...</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Read in and store as raw data bytes</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;data&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;txt&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSData</span> dataWithContentsOfFile<span style="color: #002200;">:</span>file2<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/read-files-from-the-application-bundle.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Save UIImage Object as a PNG or JPEG File</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html#comments</comments>
		<pubDate>Thu, 04 Mar 2010 13:38:18 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[jpg]]></category>
		<category><![CDATA[png]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[uiimage]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5640</guid>
		<description><![CDATA[In an earlier post on saving images from the camera to the Photo Album, a question was posed asking how to save the image to another directory other than the Photo Album. Let&#8217;s walk through one way to write a UIImage object, from the camera or otherwise, as a PNG or JPG file into the [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post on <a  href="http://iphonedevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html">saving images from the camera to the Photo Album</a>, a question was posed asking how to save the image to another directory other than the Photo Album. Let&#8217;s walk through one way to write a <strong>UIImage</strong> object, from the camera or otherwise, as a PNG or JPG file into the Documents directory.</p>
<h5>Get UIImage Data as PNG or JPEG</h5>
<p><strong>UIKit</strong> includes two C functions, <strong>UIImageJPEGRepresentation</strong> and <strong>UIImagePNGRepresentation</strong> which will return an <strong>NSData</strong> object that represents an image in JPEG or PNG format, respectively. With this information in hand, you can then use the <strong>writeToFile</strong> method of the <strong>NSData</strong> object to write the image data to a specified path.<br />
<span id="more-5640"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create paths to output images</span>
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>pngPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents/Test.png&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>jpgPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents/Test.jpg&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write a UIImage to JPEG with minimum compression (best quality)</span>
<span style="color: #11740a; font-style: italic;">// The value 'image' must be a UIImage object</span>
<span style="color: #11740a; font-style: italic;">// The value '1.0' represents image compression quality as value from 0.0 to 1.0</span>
<span style="color: #002200;">&#91;</span>UIImageJPEGRepresentation<span style="color: #002200;">&#40;</span>image, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span> writeToFile<span style="color: #002200;">:</span>jpgPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write image to PNG</span>
<span style="color: #002200;">&#91;</span>UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>image<span style="color: #002200;">&#41;</span> writeToFile<span style="color: #002200;">:</span>pngPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Let's check to see if files were successfully written...</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create file manager</span>
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Point to Document directory</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write out the contents of home directory to console</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>, <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Important note, on lines 6 and 9, the value <strong>image</strong> must be a <strong>UIImage</strong> object.</p>
<h5>Source Code</h5>
<p>I&#8217;ve changed the example <a  href="http://iphonedevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html">from the original post</a> that wrote a UIImage from the camera to the Photo Album, to write instead to the test files shown above. You can download the source code for the application here: <a href="http://iphonedevelopertips.com/wp-content/uploads/2010/03/camera2.zip">Write UIImage Object as a PNG or JPEG File</a>.</p>
<h5>Simulator Not Supported</h5>
<p>One last note, this application will only run on an actual device as the simulator does not have camera support.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Read and Write Array, Dictionary and Other Collections to Files</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html#comments</comments>
		<pubDate>Mon, 15 Feb 2010 07:26:43 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=5492</guid>
		<description><![CDATA[With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries. Read and Write Collections to File NSArray *array = &#91;NSArray arrayWithObjects: @&#34;Hefeweizen&#34;, @&#34;IPA&#34;, @&#34;Pilsner&#34;, @&#34;Stout&#34;, nil&#93;; &#160; NSDictionary *dictionary = &#91;NSDictionary dictionaryWithObjectsAndKeys: array, @&#34;array&#34;, @&#34;Stout&#34;, @&#34;dark&#34;, @&#34;Hefeweizen&#34;, @&#34;wheat&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading back both arrays and dictionaries.</p>
<h5>Read and Write Collections to File</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Pilsner&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
  array, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Stout&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dark&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hefeweizen&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;wheat&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;IPA&quot;</span>, 
  <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;hoppy&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Get path to documents directory</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, 
  NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Path to save array data</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>arrayPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
      stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;array.out&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Path to save dictionary</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>dictPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
      stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dict.out&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Write array</span>
  <span style="color: #002200;">&#91;</span>array writeToFile<span style="color: #002200;">:</span>arrayPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Write dictionary</span>
  <span style="color: #002200;">&#91;</span>dictionary writeToFile<span style="color: #002200;">:</span>dictPath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Read both back in new collections</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfFile<span style="color: #002200;">:</span>arrayPath<span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithContentsOfFile<span style="color: #002200;">:</span>dictPath<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>element <span style="color: #a61390;">in</span> arrayFromFile<span style="color: #002200;">&#41;</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Beer: %@&quot;</span>, element<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>key <span style="color: #a61390;">in</span> dictFromFile<span style="color: #002200;">&#41;</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ Style: %@&quot;</span>, key, <span style="color: #002200;">&#91;</span>dictionary valueForKey<span style="color: #002200;">:</span>key<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; 
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Verify the Output</h5>
<p>The output in the console window for the above looks as follows:<br />
<img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/02/screen.png" alt="" title="screen" width="512" height="227" class="alignnone size-full wp-image-5494" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/read-and-write-array-dictionary-and-other-collections-to-files.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Reading a plist into an NSArray</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html#comments</comments>
		<pubDate>Mon, 09 Nov 2009 07:08:31 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4498</guid>
		<description><![CDATA[Reading a plist into an NSArray This is the second tip in a series of four that is based on content from the O&#8217;Reilly book Head First iPhone Development. The first tip described how to adjust textfields that are hidden when the onscreen keyboard is shown. O’Reilly and iPhone Developer Tips are collaborating to give [...]]]></description>
			<content:encoded><![CDATA[<h5>Reading a plist into an NSArray</h5>
<p>This is the second tip in a series of four that is based on content from the O&#8217;Reilly book <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12">Head First iPhone Development</a>. The first tip described <a  target="_blank"  href="http://iphonedevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html">how to adjust textfields that are hidden when the onscreen keyboard is shown</a>.<br />
<span id="more-4498"></span></p>
<p><a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=12"><img border="0" class="alignleft" style="margin: 5px;" src="http://iphonedevelopertips.com/images/ads/Book-Headfirst.jpg" alt="Head First iPhone Development" /></a>O’Reilly and iPhone Developer Tips are collaborating to give away a free ebook (electronic copy) of Head First iPhone Development each Friday in November. You can register in this weeks drawing by referring a friend to this iPhone Developer tip – see the Refer-a-Friend button near the bottom of this post. You can read more about last weeks winner, Bill Mietelski, <a href="http://iphonedevelopertips.com/giveaway/book-giveaway-head-first-iphone-development-winner-announcement.html">on this post</a>.</p>
<p>This tip, based on content in Chapter 4 &#8211; Multiple View, shows how to read a plist from a file in the resource bundle, into an array.</p>
<h5>Hardcoded Array</h5>
<p>Based on the example in the book, let&#8217;s assume we are keeping a list of mixed drinks inside our application. Somewhere in the initialization process we might write something like the following to build an array of <strong>NSString</strong> values:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Firecracker&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Lemon Drop&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mojito&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show the string values  </span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> array<span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>Creating a plist</h5>
<p>Although the code above works, a better solution would be to keep the list of values in a plist as part of the resource bundle, then read the plist into an array. </p>
<p>To create plist, in Xcode, right click on <strong>Resources</strong> then select <strong>Add</strong>, <strong>New File</strong>, <strong>Mac OS X</strong>, <strong>Resource</strong>, <strong>Property List</strong>. Name the file <strong>DrinkArray.plist</strong>. Change the root type to <strong>Array</strong> and then add the four string values shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/plist.png" /></p>
<h5>Reading a plist into an NSArray</h5>
<p>Reading the plist into an array is straight forward:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Path to the plist (in the application bundle)</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span>
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DrinkArray&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Build the array from the plist  </span>
<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>array2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show the string values  </span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #a61390;">in</span> array2<span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;--%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<h5>plists and Other Data Types</h5>
<p>One of the benefits of working with plists, is that other types, for example <strong>NSDictionary</strong>, can be read from a plist. This means that you can create more complex data collections in a plist, such as an array of dictionaries.</p>
<p>For example, the plist below is also an array, however, the third value in the array is a dictionary. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/11/plist2.png" /></p>
<p>With plists you can build various data types needed by your application and read those values directly into your program.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/reading-a-plist-into-an-nsarray.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iPhone File System: Creating, Renaming and Deleting Files</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html#comments</comments>
		<pubDate>Tue, 27 Oct 2009 11:04:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=4380</guid>
		<description><![CDATA[The iPhone provides a comprehensive set of operations for working with files and directories. NSFileManager includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc). Point to the Documents Directory Each application has its own sandbox in which you can read/write files. Files [...]]]></description>
			<content:encoded><![CDATA[<p>The iPhone provides a comprehensive set of operations for working with files and directories. <strong>NSFileManager</strong> includes methods for querying the contents of directories, creating, renaming and deleting contents, as well as getting/setting file attributes (readable, writeable, etc).</p>
<h5>Point to the Documents Directory</h5>
<p>Each application has its own sandbox in which you can read/write files. Files written to the sandbox are persistent across invocations of the application, including across application updates.</p>
<p>You can locate the Documents directory in the sandbox as shown below:<br />
<span id="more-4380"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// For error information</span>
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create file manager</span>
<span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileMgr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Point to Document directory</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>documentsDirectory <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Creating a File</h5>
<p>With the documents directory available, we can now use that path to create a new file in the sandbox and write a few lines of text:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// File we want to create in the documents directory </span>
<span style="color: #11740a; font-style: italic;">// Result is: /Documents/file1.txt</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
         stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file1.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// String to write</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iPhone Developer Tips<span style="color: #2400d9;">\n</span>http://iPhoneDevelopTips,com&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Write the file</span>
<span style="color: #002200;">&#91;</span>str writeToFile<span style="color: #002200;">:</span>filePath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span> 
         encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>We build a path to the file we want to create (file1.txt), initialize a string to write into the file, and write out the contents. The last line shows a directory listing of what is in the Documents directory after we create the file, see the figure below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr1.png" /></p>
<h5>Renaming a File</h5>
<p>To rename a file we move the file to a new path. The code below creates the destination path we are after, requests to move the file, and shows the Documents directory after the move.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Rename the file, by moving the file</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>filePath2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsDirectory 
                                 stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file2.txt&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Attempt the move</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr moveItemAtPath<span style="color: #002200;">:</span>filePath toPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to move file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>, 
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>After moving the file, the output should look similar to the image below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr2.png" /></p>
<h5>Deleting a File</h5>
<p>To round out this tip, let&#8217;s look at how to delete a file:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Attempt to delete the file at filePath2</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>fileMgr removeItemAtPath<span style="color: #002200;">:</span>filePath2 error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Unable to delete file: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show contents of Documents directory</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Documents directory: %@&quot;</span>,
         <span style="color: #002200;">&#91;</span>fileMgr contentsOfDirectoryAtPath<span style="color: #002200;">:</span>documentsDirectory error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Once the file is deleted, as expected, the Documents directory is now empty:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/10/filemgr3.png" /></p>
<p>These examples touch the surface of working with files. Check out the documentation for <strong>NSFileManager</strong> for all the specifics.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/iphone-file-system-creating-renaming-and-deleting-files.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get Users Home Directory &#8211; Part 2</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html#comments</comments>
		<pubDate>Mon, 26 Jan 2009 04:47:22 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[home directory]]></category>
		<category><![CDATA[plist]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1469</guid>
		<description><![CDATA[In the previous tip on working with plists, I showed an example of how to create a path to a file to be stored in the /Documents folder. In this tip I want to show an example that arrives at the same end result, however, I&#8217;ll use methods which will eliminate the hardcoding of paths [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://iphonedevelopertips.com/data-file-management/get-users-home-directory.html">previous tip</a> on working with plists, I showed an example of how to create a path to a file to be stored in the <em>/Documents</em> folder.</p>
<p>In this tip I want to show an example that arrives at the same end result, however, I&#8217;ll use methods which will eliminate the hardcoding of paths as shown in the first example.<br />
<span id="more-1469"></span></p>
<p>To quickly recap, the intention was to create a file named <em>User.plist</em> and store that file in the user&#8217;s documents folder. In the previous post I specified the full file path I was after (/Documents/User.plist) and then appended that path to the user&#8217;s home directory. </p>
<p>Here&#8217;s another approach:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, 
    NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>paths count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>plistPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> 
                                 stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;User.plist&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Check for existence of the file</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>plistPath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
    ... 
  <span style="color: #a61390;">else</span>
    ...
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>With this approach I request the path to the documents directory through a call to <em>NSSearchPathForDirectoriesInDomains</em> by specifying <em>NSDocumentDirectory</em> as the search path. To build the fullpath I first retrieve the path to the documents directory ([paths objectAtIndex:0]) and append the filename that I am after through a call to <em>stringByAppendingPathComponent</em>. The later call will prepend a path separator and append the filename requested. </p>
<p>The end result, with no hardcoding: /Documents/User.plist</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Users Home Directory &#8211; Part 1</title>
		<link>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html</link>
		<comments>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html#comments</comments>
		<pubDate>Fri, 23 Jan 2009 14:45:03 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Data / File Management]]></category>
		<category><![CDATA[home directory]]></category>
		<category><![CDATA[plist]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1455</guid>
		<description><![CDATA[In a recent application I needed to create several property list files (plists) to store application information. I opted to store the files in the folder named &#8220;Documents&#8221; off the home folder. Here is the code I used to specify the path where I wanted the files written and the code to build an NSString [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent application I needed to create several property list files (plists) to store application information. I opted to store the files in the folder named &#8220;Documents&#8221; off the home folder.</p>
<p>Here is the code I used to specify the path where I wanted the files written and the code to build an NSString with the full path.<br />
<span id="more-1455"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Folder and file name</span>
<span style="color: #6e371a;">#define USER_PLIST_FULLPATH @&quot;/Documents/User.plist&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Path to the user property list</span>
<span style="color: #400080;">NSString</span>  <span style="color: #002200;">*</span>plistPath;
plistPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSHomeDirectory<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> stringByAppendingString<span style="color: #002200;">:</span>USER_PLIST_FULLPATH<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Check for existence of the file</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>plistPath<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  ...
<span style="color: #a61390;">else</span>
  ...</pre></td></tr></table></div>

<p>The key to making this work is NSHomeDirectory() a low level C function that returns the path to the current user’s home directory. See the document <em>Low-Level File Management Programming Topics</em> for the specifics on working with the file system.</p>
<p>In the next post I&#8217;ll show another means to generate paths to files, arguably a better approach as it uses the provided frameworks for building paths (versus hardcoding as shown here).<br />
<strong><br />
Important Note:</strong> There is a <em>/Documents</em> directory created in the sandbox for each iPhone application. In other words, the plist file that I created above will not clash with a file of the same name in another iPhone application. </p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/data-file-management/get-users-home-directory.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
