<span id="Label3"></p><span style="font-size: 18px;"><span style="font-size: 18px;">1. </span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain">NSString *path = [[NSBundle mainBundle] pathForResource:@</code><code class="string">"文件名"</code> <code class="plain">ofType:@</code><code class="string">"plist"</code><code class="plain">];</code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain">// 文件数据类型是array<br></code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain">NSArray *array=[NSArray arrayWithContentsOfFile:path];<br></code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain">//文件数据类型是*dictionary<br></code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain">NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];<br></code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain"> </code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;"> </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">//1. Creating a plist file</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">Nsarray *paths=nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes);</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">nsstring *path=[paths objectatindex:0];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">NSLog (@ "path =%@", path);</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">nsstring *filename=[path stringbyappendingpathcomponent:@ "test.plist"]; </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">nsfilemanager* fm = [nsfilemanager defaultmanager];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">[FM createfileatpath:filename Contents:nil attributes:nil]; </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">//nsdictionary* dic = [nsdictionary dictionarywithcontentsoffile:plistpath];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;"> </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">//create a dic and write it into the plist file</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">nsdictionary* dic = [nsdictionary dictionarywithobjectsandkeys:@ "sina", @ "1", @ "163", @ "2", nil];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">[dic writetofile:filename atomically:yes];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;"> </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">//read File</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">nsdictionary* dic2 = [nsdictionary dictionarywithcontentsoffile:filename];</span></span><span style="font-size: 18px;"><span style="font-size: 18px;">NSLog (@ "dic is:%@", dic2);</span></span><span style="font-size: 18px;"><span style="font-size: 18px;"> </span></span><span style="font-size: 18px;"><span style="font-size: 18px;"><code class="plain"> </code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;">2. Use of Nsuserdefaults</span></span><br><span style="font-size: 18px;"><span style="font-size: 18px;">There are several ways to create a user defaults method, the simplest way to create it quickly:</span></span><br><span style="font-size: 18px;"><span style="font-size: 18px;">nsuserdefaults *accountdefaults = [nsuserdefaults standarduserdefaults];</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;">add data to user Defaults:</span></span><br><span style="font-size: 18px;"><span style="font-size: 18px;">[accountdefaults setObject:nameField.text forkey:userdefaultnamekey];</span></span><br><span style="font-size: 18px;"><span style="font-size: 18px;">You can also add the basic data type int, float, bool, etc., have the corresponding method</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;">[accountdefaults Setbool:yes forkey:userdefaultboolkey];</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;">get data from user Defaults:</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;">[accountdefaults objectforkey:ncuserdefaultnamekey]</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;"> </span></span><br><span style="font-size: 18px;"><span style="font-size: 18px;">[accountdefaults boolforkey:userdefaultboolkey];</span></span><br><br><span style="font-size: 18px;"><span style="font-size: 18px;">important:<span style="color: #ff0000;"> Nsuserdefaults is very useful, does not require the user to set Nsuserdefaults global variables in the program, where to use the Nsuserdefaults data, Then create a nsuserdefaults object, then read or Write. </span></span></span><br><span style="font-size: 18px;">for <span style="font-size: 18px;">the same keyword corresponding to the object or data, it can be rewritten, after rewriting the keyword corresponding to the new object or data, the old object or data will be automatically cleaned up. </span></span><span style="font-size: 18px;"><span style="font-size: 18px;">3 questions about Nsuserdefaults can't save data</span></span><span style="font-size: 18px; font-style: italic; line-height: 1.5;"><span style="font-size: 18px; font-style: italic; line-height: 1.5;">If You terminate your app is pressing the home button (in the Simulator or on the device), your User Defaults would get Saved.</span></span><p><p><span style="font-size: 18px;">If you press the home button to terminate your app (real machine or simulator), your value will be Saved.</span></p></p><p><p><span style="font-size: 18px;">If you terminate your app. pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults <em>might get saved, But there's a good chance they won ' T. nsuserdefaults persists any changes periodically, and if you terminate The process before they ' ve been persisted, they ' ll be gone. You can force the save by Calling:</em></span></p></p><pre><pre><span style="font-size: 18px;"><code>[[NSUserDefaults standardUserDefaults] synchronize];</code></span></pre></pre><span style="font-size: 18px;"><span style="font-size: 18px;"><code> </code></span></span><span style="font-size: 18px;"><span style="font-size: 18px;">If you terminate your app in Xcode (on a simulator or on a real machine), your value sometimes (terminate the process before they ' ve been persisted (not knowing what the ghost Is) will not be saved. But</span></span><span style="font-size: 18px; line-height: 1.5;"><span style="font-size: 18px; line-height: 1.5;">You can use the following code to force the Save:</span></span><pre><pre><span style="font-size: 18px;"><code>[[NSUserDefaults standardUserDefaults] synchronize];</code></span></pre></pre><p><p>How iOS saves on-premises data</p></p></span>
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