[Objetive-C Basics] read and write the. plist File

Source: Internet
Author: User
Code

  Take the name, phonenumber, and age fields as an example. Input data in the text box on the iPhone, load the data into the array, and write the array to The. plist file.

Write operation

Nsstring * Name = [Txt1 text];
Nsnumber * Phonenumber = [[Nsnumber alloc] initwithint: [[txt2 text] intvalue];
Nsnumber * Age = [[Nsnumber alloc] initwithint: [[txt3 text] intvalue];

Nsmutablearray * Array = [[Nsmutablearray alloc] init];
[Array addobject: Name];
[Array addobject: phonenumber];
[Array addobject: Age];

Nsarray * Paths = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * Path = [Paths objectatindex: 0 ];
Nsstring * Filename = [Path stringbyappendingpathcomponent: @" Personal. plist " ];

[Array writetofile: Filename atomically: Yes];
[Array release];



Read operations

Nsarray * Paths = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * Path = [Paths objectatindex: 0 ];
Nsstring * Filename = [Path stringbyappendingpathcomponent: @" Personal. plist " ];

Nsmutablearray * Array = [[Nsmutablearray alloc] initwithcontentsoffile: Filename];

Txt1.text = [Array objectatindex: 0 ];
Txt2.text = [[Nsstring alloc] initwithformat: @" % D " , [[Array objectatindex: 1 ] Intvalue];
Txt3.text = [[Nsstring alloc] initwithformat: @" % D " , [[Array objectatindex: 2 ] Intvalue];

[Array release];

Key point: I downloaded the demo written to plist in the Forum and found that the plist file in the project has not been changed. Is this used to save temporary data? Not written to app?

We recommend that you print out the path if you do not understand it later.

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.