iOS Development Memo: Attribute list file Data persistence

Source: Internet
Author: User

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:@"contacts.plist"]; Nserror*error; BOOL Success=[filemanager Copyitematpath:defaultdbpath Topath:writabledbpath error:&ERROR]; if(!success) {NSAssert1 (0,@"Error writing to file: '%@ '", [Error localizeddescription]); }    }}//get the full path of files placed in the sandbox documents directory-(NSString *) applicationdocumentsdirectoryfile{NSString*documentdirectory=[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject]; NSString*path=[documentdirectory stringbyappendingpathcomponent:@"contacts.plist"]; returnpath;}

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, which is the collection of all the data in the property list fileNsmutablearray *array=[[Nsmutablearray Alloc]initwithcontentsoffile:path]; //add a new record to the arrayNsdictionary *newcontact=[nsdictionary dictionarywithobjects:@[contact. Title,contact. Type] forkeys:@[@"Title",@"Type"]];        [Array addobject:newcontact]; //Delete a record in an array[Array Removeobjectatindex:0]; //Delete all records in array[Array removeallobjects];  for(nsdictionary* dictinchArray) {        //with a For loop, find the data item that needs to be modified, make the modification data[Dict SetValue:@"Test"Forkey:@"Title"]; }        //to write an array back into the property 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.