Creation of plist in iOS, data write and read

Source: Internet
Author: User

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;      &nbsp }        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

Related Article

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.