iOS Development Memo: Attribute list file Data persistence

Source: Internet
Author: User

Original: http://www.cnblogs.com/wzk89/p/3939782.html

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.

Nsarray class commonly read and write property list file method :

+arraywithcontentsoffile: Class-level construction method for reading data from a property list file and creating a Nsarray object.

-initwithcontentsoffile: An instance construction method that is used to read data from a property list file and create a Nsarray object.

-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.

Nsdictionary class commonly read and write property list file method:

+dictionarywithcontentsoffile: Class-level construction method for reading data from a property list file and creating a Nsdictionary object.

-initwithcontentsoffile: An instance construction method that is used to read data from a property list file and create a Nsdictionary object.

-writetofile:atomically: This method writes the Nsdictionary object to the property list file.

Attribute list file data Persistence specific method, you can refer to the following implementation methods:

If you created a contacts.plist file manually in the project and added a few data to the file, as shown in.

You can also create a plist file directly from the code.

The next thing you need to do is copy the data from the project resource's Contacts.plist file to the sandbox documents directory.

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.    -(void) createeditablecopyofdatabaseifneeded{Nsfilemanager *filemanager=[nsfilemanager DefaultManager];        NSString *writabledbpath=[self Applicationdocumentsdirectoryfile];    BOOL Dbexits=[filemanager Fileexistsatpath:writabledbpath]; if (!dbexits) {nsstring *defaultdbpath=[[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@ "Cont                Acts.plist "];        Nserror *error;                BOOL Success=[filemanager Copyitematpath:defaultdbpath Topath:writabledbpath error:&error];        if (!success) {NSAssert1 (0,@ "error written to file: '%@ '", [Error localizeddescription]); }}//gets the full path of the file placed in the sandbox documents directory-(NSString *) applicationdocumentsdirectoryfile{nsstring *documentdirectory=[    Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];       NSString *path=[documentdirectory stringbyappendingpathcomponent:@ "Contacts.plist"]; Return Path;} 

in the Createeditablecopyofdatabaseifneeded method :

Nsfilemanager CopyItemAtPath:toPath:error: Method to implement File replication.

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.

in the Applicationdocumentsdirectoryfile method :

stringByAppendingPathComponent: The ability to append file names to the directory and return the full file path.

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:

NSString *path=[self Applicationdocumentsdirectoryfile];        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=[[nsmutablearray alloc] Initwithcontentsoffile:path];        Add a new record to the array    nsdictionary *newcontact=[nsdictionary dictionarywithobjects:@[contact. Title,contact. Type] forkeys:@[@ "Title", @ "Type"];    [Array addobject:newcontact];        Delete a record in array    [array removeobjectatindex:0];        Delete all records in array    [array removeallobjects];        For (nsdictionary* dict in array) {        //through A For loop, locate the data item to be modified, modify the data        [dict setvalue:@ "Test" forkey:@ "Title"];    }        //write the array back into the    attribute list file [Array Writetofile:path atomically:yes];

Note: After completion, you need to select the Product->clean menu item to clear some recompile.

iOS Development Memo: Attribute list file Data persistence

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.