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.