iOS development--data persistence oc & (ii) attribute list

Source: Internet
Author: User
<span id="Label3"></p><span style="color: #333333;"><span style="color: #333333;"><span style="color: #333333;">Property List</span></span></span><p><p>A property list file is an XML file that can be converted from one property list file to another, such as arrays and dictionaries in the foundation Framework.</p></p><p><p><span style="color: #ff6600;"><strong>Nsarray class commonly read and write property list file method</strong> :</span></p></p><p><p>+arraywithcontentsoffile: Class-level Construction method for reading data from a property list file and creating a Nsarray object.</p></p><p><p>-initwithcontentsoffile: an instance construction method that is used to read data from a property list file and create a Nsarray object.</p></p><p><p>-writetofile:atomically: This method writes the Nsarray object to the property list file, The first parameter is the file name, the second parameter is whether to use the auxiliary file, if yes, writes to a secondary file, and then the secondary file is renamed to the target File. If no, it is written directly to the destination File.</p></p><p><p><span style="color: #ff6600;"><strong>Nsdictionary class commonly read and write property list file Method:</strong></span></p></p><p><p>+dictionarywithcontentsoffile: Class-level Construction method for reading data from a property list file and creating a nsdictionary Object.</p></p><p><p>-initwithcontentsoffile: an instance construction method that is used to read data from a property list file and create a nsdictionary object.</p></p><p><p>-writetofile:atomically: This method writes the Nsdictionary object to the property list File.</p></p><p><p>Attribute list file data persistence specific method, you can refer to the following implementation methods:</p></p><p><p>If you created a contacts.plist file manually in the project and added a few data to the file, as shown in.</p></p><p><p>You can also create a plist file directly from the Code.</p></p><p><p></p></p><p><p>The next thing you need to do is copy the data from the project Resource's contacts.plist file to the sandbox documents Directory.</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008000;">//<span style="color: #008000;">The file is preprocessed to determine if there is a plist file in the documents directory, and if it does not exist, copy one from the resource directory. -(<span style="color: #0000ff;">void<span style="color: #000000;">) Createeditablecopyofdatabaseifneeded{nsfilemanager *filemanager=<span style="color: #000000;">[nsfilemanager defaultmanager]; NSString *writabledbpath=<span style="color: #000000;">[self applicationdocumentsdirectoryfile]; BOOL dbexits=<span style="color: #000000;">[filemanager fileexistsatpath:writabledbpath];<span style="color: #0000ff;">If (!<span style="color: #000000;">Dbexits) {nsstring *defaultdbpath=[[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:<span style="color: #800000;">@"<span style="color: #800000;">Contacts.plist<span style="color: #800000;">"<span style="color: #000000;">]; Nserror *<span style="color: #000000;">Error BOOL Success=[filemanager Copyitematpath:defaultdbpath Topath:writabledbpath error:&<span style="color: #000000;">error];<span style="color: #0000ff;">If (!<span style="color: #000000;"><span style="color: #000000;">Success) {NSAssert1 (<span style="color: #800080;">0,<span style="color: #800000;">@ " <span style="color: #800000;">error writing to file: '%@ '<span style="color: #800000;">"<span style="color: #000000;">, [error localizeddescription]);}}} <span style="color: #008000;">// <span style="color: #008000;">Get the full path to the file placed in the sandbox documents directory-(nsstring *<span style="color: #000000;">) applicationdocumentsdirectoryfile{nsstring * documentdirectory=<span style="color: #000000;">[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) lastObject ]; NSString *path=[documentdirectory stringbyappendingpathcomponent:<span style="color: #800000;">@ "<span style="color: #800000;">contacts.plist<span style="color: #800000;">"<span style="color: #000000;">]; <span style="color: #0000ff;">return<span style="color: #000000;"> path;}</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p class="p1"><p class="p1"><span style="color: #ff6600;"><strong>in the Createeditablecopyofdatabaseifneeded method</strong> :</span></p></p><p class="p1"><p class="p1">Nsfilemanager CopyItemAtPath:toPath:error: method to implement file Replication.</p></p><p class="p1"><p class="p1">NSAssert1 is a macro provided by the Foundation framework that throws an exception in the case of an assertion failure, similar to Nsassert and NSAssert2.</p></p><p class="p1"><p class="p1"><span style="color: #ff6600;"><strong>in the Applicationdocumentsdirectoryfile method</strong> :</span></p></p><p class="p1"><p class="p1">Stringbyappendingpathcomponent: the ability to append file names to the directory and return the full file path.</p></p><p class="p1"><p class="p1">After successfully generating the plist file in the sandbox documents directory, you can add, delete, change, and check the Operation. Refer to the following code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>NSString *path=<span style="color: #000000;">[self applicationdocumentsdirectoryfile];<span style="color: #008000;">//<span style="color: #008000;">Reads the contents of the property list file into the array variable, that is, gets all the data sets in the property list file Nsmutablearray *array=<span style="color: #000000;">[[nsmutablearray alloc]initwithcontentsoffile:path];<span style="color: #008000;">//<span style="color: #008000;">Add a new record to the array nsdictionary *newcontact=[nsdictionary Dictionarywithobjects:@[contact. Title,contact. Type] forkeys:@[<span style="color: #800000;">@"<span style="color: #800000;">Title<span style="color: #800000;">",<span style="color: #800000;">@"<span style="color: #800000;">Type<span style="color: #800000;">"<span style="color: #000000;">]]; [array addobject:newcontact];<span style="color: #008000;">//<span style="color: #008000;"><span style="color: #008000;"> Delete one record in array [array removeobjectatindex:<span style="color: #800080;">0<span style="color: #000000;">]; <span style="color: #008000;">//<span style="color: #008000;"> Delete all records in array <span style="color: #000000;"> [array removeallobjects]; <span style="color: #0000ff;">for (nsdictionary* dict <span style="color: #0000ff;">in <span style="color: #000000;"> Array) {<span style="color: #008000;">//<span style="color: #800000;" ) through the for loop, locate data item that needs to be modified, and make modified [dict setvalue:<span>@ "<span style=" color: #800000; ">test<span style=" color: #800000; ">" forkey:<span style="color: #800000;">@ " Span style= "color: #800000;" >title<span style="color: #800000;"> "<span style="color: #008000;" ];} <span>//<span style="color: #008000;" "to write the array back into property list file [array writetofile:path atomically:yes]; < span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Note: after completion, you need to select the Product->clean menu item to clear some Recompile.</p></p><p><p>iOS development--data persistence oc & (ii) attribute list</p></p></span>
Related Article

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.