<?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>Chris-Software.com &#187; data</title>
	<atom:link href="http://chris-software.com/index.php/tag/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-software.com</link>
	<description>iPhone, iPod touch games, Objective-C Tutorials, krzysztofrutkowski.com</description>
	<lastBuildDate>Tue, 29 Mar 2011 09:58:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Saving data</title>
		<link>http://chris-software.com/index.php/2009/04/26/saving-data/</link>
		<comments>http://chris-software.com/index.php/2009/04/26/saving-data/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 13:25:38 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[restore data]]></category>
		<category><![CDATA[save]]></category>
		<category><![CDATA[saving data]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=611</guid>
		<description><![CDATA[Saving data is an obvious part of most of your applications. In games you probably need to save records &#8211; names, scores, times, settings like the music volume. In applications you should save enabled options. If your application is big it&#8217;s great if you occasionally save the whole state / progress and even user terminates [...]]]></description>
			<content:encoded><![CDATA[<p>Saving data is an obvious part of most of your applications. In games you probably need to save records &#8211; names, scores, times, settings like the music volume. In applications you should save enabled options. If your application is big it&#8217;s great if you occasionally save the whole state / progress and even user terminates the app it can be restored during next launch.</p>
<p>Here you will learn how to save data locally on your device / simulator. It&#8217;s not as difficult it appears to be. Of course you could use an SQLite database and write a controller to inser / update / delete records but it&#8217;s much more advanced. From this tutorial you will learn how to use <em>NSUserDefaults</em> to save whatever you need.</p>
<p>The syntax is simple. To save your data use this code:</p>
<pre><span><span>	</span></span>NSUserDefaults<span> *defaults = [</span>NSUserDefaults<span> </span><span>standardUserDefaults</span><span>];
<span>	</span>[defaults <span>setInteger</span>:<span>5</span> <span>forKey</span>:<span>@"myNumber"</span>];
<span>	</span>[defaults <span>setBool</span>:<span>YES</span> <span>forKey</span>:<span>@"switchEnabled"</span>];
<span>	</span>[defaults <span>setFloat</span>:<span>0.24</span> <span>forKey</span>:<span>@"sliderValue"</span>];
<span><span>	</span>[defaults </span><span>setObject</span><span>:</span>@"Chris"<span> </span><span>forKey</span><span>:</span>@"myName"<span>];
<span>	</span>[defaults <span>synchronize</span>];</span></span></pre>
<p>As you see above I have saved 4 values. <em>myNumber</em> is an <em>integer</em> and <em>=5</em>. <em>switchEnabled</em> is a <em>boolean</em> with logic value <em>YES / true</em>. <em>sliderValue</em> is a <em>float</em> and <em>=0.24</em> and <em>myName</em> is an <em>object</em> &#8211; it&#8217;s a <em>NSString</em> and it holds English equivalent of my name <img src='http://chris-software.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Now, to get values from any <em>key</em> you can use only one line of code for example:</p>
<pre><span><span>	</span>int anInteger = [[</span><span>NSUserDefaults</span><span> </span>standardUserDefaults<span>] </span>integerForKey<span>:</span><span>@"myInteger"</span><span>];</span></pre>
<div>Since now <em>anInteger=5</em>.</div>
<div>If you try to load a value that doesn&#8217;t exist you will get <em>0</em> for <em>integers</em>, <em>doubles</em>, <em>floats</em>, <em>NO</em> for <em>booleans</em> and <em>nil</em> for <em>objects</em>.</div>
<p>I prepared for you a project, where 3 different values are saved during any change, and loaded with the launch of the application. You can change there slider value, enter your name, and press the button &#8211; whenever the button is pressed the value below grows. You can quit and launch the application and see what happens.</p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/savesimulator.png"><img class="size-medium wp-image-612 aligncenter" title="savesimulator" src="http://chris-software.com/wp-content/uploads/2009/04/savesimulator-161x300.png" alt="savesimulator" width="161" height="300" /></a></p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/savingdata.zip"><img class="alignnone size-full wp-image-564" title="xcodeproj" src="http://chris-software.com/wp-content/uploads/2009/04/xcodeproj.png" alt="xcodeproj" width="128" height="128" /></a></p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/savingdata.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/26/saving-data/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

