Three ways to store user data for iOS applications

Source: Internet
Author: User
<span id="Label3"></p><p><p>This article demonstrates the iOS application user data storage mechanism through preferences, plist files, archive three Ways.</p></p><p><p>first, we create a person class and define three member properties to store the data</p></p><pre><pre>@property (nonatomic,copy) NSString *<span style="color: #000000;">name; @property (nonatomic,assign) nsinteger age; @property (nonatomic,assign) BOOL isTrue;</span></pre></pre><p><p></p></p><p><p>first, by preference Settings</p></p><p><p>1. Storing data via preferences</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the object of the preference setting</span></span>Nsuserdefaults *defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">tell the system what you want to deposit.</span></span>[defaults setobject:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Xiao Ming</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [defaults setinteger:</span></span><span style="color: #800080;"><span style="color: #800080;"></span> -</span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> age</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [defaults Setbool:yes forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">isTrue</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">data Synchronization (force Write)</span></span>[defaults synchronize];</pre><p><p></p></p><p><p>2. reading data via Preferences</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">gets the Nsuserdefaults object, which is a singleton</span></span>Nsuserdefaults *defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get Data</span></span>NSString *name = [defaults objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsinteger</span> age</span>= [defaults integerforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> age</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; BOOL isTrue</span></span>= [defaults boolforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">isTrue</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Validation</span></span>NSLog (<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">is%@%@ years old?%d</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, name,@ (age), isTrue);</pre><p><p></p></p><p><p>Ii. adoption of the plist document</p></p><p><p>1. Writing data through plist files</p></p><p><p>Note: The file attributes list is an xml-formatted file that expands to the name Plist.</p></p><p><p>If the object is nsstring, nsdictionary, nsarray, nsdata, nsnumber, and so on, you can use the Writetofile:atomically: method to write the object directly to the property list File.</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">define a macro in advance to get the path</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>PATH [[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) lastobject] stringbyappendingpathcomponent:@ "data.txt"]<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">add the stored content</span></span>Nsarray *arry = @[<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">John Doe</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Lao Wang</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Xiao Ming</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Deposit Locally (atomically thread Protection)</span></span><span style="color: #000000;"><span style="color: #000000;">[arry Writetofile:path atomically:yes]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, PATH);</pre><p><p></p></p><p><p>2. reading data via plist file</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">reading data from a plist file</span> *array =<span style="color: #000000;"> [nsarray arraywithcontentsoffile:path]; </span> <span style="color: #000000;"> </span> <span style="color: #0000ff;"></span> for <span style="color: #0000ff;">inch</span> <span style="color: #000000;">Array) { NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@</span><span style="color: #800000;">"</span><span style="color: #000000;">, str); }</span></pre></pre><p><p></p></p><p><p>third, through the Archiving and reconciliation files</p></p><p><p>1. Writing data through an archive file</p></p><p><p>The essence of archiving is to archive a Nsdictionary object into a list of plist Attributes.</p></p><p><p>If you use an archive file, you must introduce the Nscoding protocol to the person class and implement the Encodewithcoder: and Initwithcoder: methods.</p></p><p><p>Description</p></p><p><p>Encodewithcoder:</p></p><p><p>This method is called every time the object is Archived.</p></p><p><p>In this method, you typically specify how to archive each instance variable in an object, and you can use the Encodeobject:forkey: method to archive instance Variables.</p></p><p><p>Initwithcoder:</p></p><p><p>This method is called every time the object is recovered (decoded) from the File.</p></p><p><p>In this method, you typically specify how to decode the data in a file as an instance variable of an object, and you can use the Decodeobject:forkey method to decode the instance Variable.</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">implementation file for the person class</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">archiving is just a process that tells the system which properties you want to store objects</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Encodewithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) Acoder{[acoder encodeObject:self.name forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [acoder encodeInteger:self.age forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> age</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [acoder encodeBool:self.isTrue forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">isTrue</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">];}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">anti-archiving is just a process, and this method tells the system which properties you want to get the object</span></span>-(instancetype) initwithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) adecoder{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self =<span style="color: #000000;"><span style="color: #000000;">[super Init]) {self.name</span></span>= [adecoder decodeobjectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.age</span></span>= [adecoder decodeintegerforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> age</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.istrue</span></span>= [adecoder decodeboolforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">isTrue</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">;}</span></span></pre><p><p></p></p><p><p>2. Writing data using an archive method</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Write Data</span> Person *p =<span style="color: #000000;"> [[person alloc]init]; </span> <span style="color: #800000;">@"</span> <span style="color: #800000;">Xiao Wang</span> <span style="color: #800000;">"</span> <span style="color: #000000;">; </span> <span style="color: #800080;"></span> - <span style="color: #000000;">; </span> =<span style="color: #000000;"> NO;</span> <span style="color: #008000;">//</span> the <span style="color: #008000;">premise of storing the archive is that the current object must comply with the Nscoding protocol</span> [nskeyedarchiver archiverootobject:p tofile:path];</pre></pre><p><p></p></p><p><p><span style="line-height: 1.5;">3. Read data via anti-archive (solution File)</span></p></p><pre><pre>Person *p =<span style="color: #000000;"> [nskeyedunarchiver unarchiveobjectwithfile:path]; NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@,%@,%d</span><span style="color: #800000;">"</span>, p.name,@ (p.age), p.istrue);</pre></pre><p><p></p></p><p><p>Three ways to store user data for iOS applications</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.