iOS plist creation, data write and read function create a test.plist file, textinput as input, Displaylabel as the display, there is a button to trigger the retention program Triggerstorage;
-(void) triggerstorage{ Displaylabel.text = textinput.text; Nsarray *paths =nssearchpathfordirectoriesindomains (nsdocumentdirectory,nsuserdomainmask,yes); NSString *path=[ Paths objectAtIndex:0]; nsstring *filename=[path stringbyappendingpathcomponent:@ "Test.plist"]; //Get path nsdictionary* dic2 = [Nsdictionary dictionarywithcontentsoffile:filename ]; //Read Data NSLOG (@ "Dic2 is:%@", Dic2); //Create a dic, write to the plist file nsdictionary* dic = [nsdictionary dictionarywithobjectsandkeys:displaylabel.text,@ "IP", nil]; Write Data [dic Writetofile:filename Atomically:yes]; } //Implement viewdidload to does additional setup after loading the view, typically from a nib.-(v OID) viewdidload{ Nsmutablearray *resultdata; Nsarray *paths= NssearchpathfordirectoriesinDomains (nsdocumentdirectory,nsuserdomainmask,yes); NSString *path=[paths objectatindex:0]; NSLog (@ "path =%@", path); nsstring *filename=[path stringbyappendingpathcomponent:@ "Test.plist"]; //Read file nsdictionary* dic2 = [Nsdictionary dictionarywithcontentsoffile:filename]; NSLog (@ "dic is:%@", Dic2); if (Dic2 = nil) { //1. Create a plist file & nbsp nsfilemanager* fm = [Nsfilemanager defaultmanager]; [FM Createfileatpath:filename Conte Nts:nil Attributes:nil]; } else { RESULTDATA=[DIC2 OB jectforkey:@ "IP"]; if ([Dic2 count] > 0) { &NBS P Displaylabel.text = resultdata;   } else { Displaylabel.text = @ ""; } } Self.navigationItem.rightBarButtonItem = [[[Uibarbuttonit] EM alloc] //button initialization and trigger condition settings initwithtitle:@ "save" , &N Bsp style:UIBarButtonItemStylePlain & nbsp target:self & nbsp , &NB Sp; action: @selector (triggerstorage)] autorelease]; [Super Viewdidload];} --------------------------------------------------------------------------------------------------------------- ---------------------------------------------Reference Code: plist file read/write //1. Create a plist file Nsarray *paths=nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES); www.2cto.com NSString *path=[paths objectAtIndex:0]; NSLog (@ "path =%@", Path); nsstring *filename=[path stringbyappendingpathcomponent:@ "Test.plist"]; nsfilemanager* fm = [Nsfilemanager defaultmanager]; [FM Createfileatpath: FileName Contents:nil Attributes:nil]; //nsdictionary* dic = [Nsdictionary dictionarywithcontentsoffile:plistpath];& nbsp //Create a dic, write to plist file nsdictionary* dic = [Nsdictionary dictionarywithobjectsandkeys:@ "Sina", @ "1", @ "163", @ "2",nil]; [dic writetofile:filename atomically:yes]; /Read file nsdictionary* dic2 = [nsdictionary dictionarywithcontentsoffile:filename]; NSLog (@ "dic is:%@", Dic2); related articles: http://blog.csdn.net/ Totogo2010/article/details/7634185http://www.2cto.com/kf/201302/188061.html
Creation of plist in iOS, data write and read