Write/read/delete of IOS plist file operations

Source: Internet
Author: User

  • Writing data to a plist file

  •   //Get Path Object     NSArray *pathArray =  Nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes);     nsstring *path = [patharray objectatindex:0];    //get the full path to the file     nsstring *filepatch = [path stringbyappendingpathcomponent:@ " Xiaoxi.plist "];        //above 3 sentences can be written this sentence//    nsstring  *filepatch = [[nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask,  yes) objectatindex:0]stringbyappendingpathcomponent:@ "Xiaoxi.plist"];    //     nslog (@ "------filepath---%@", filepatch);    /*      *      below is my plist path, click in the blank of the desktop, go to-hold the option-resource library-developer-coresimulator-devices ...... Just follow the path below to find where plist is located      *    /users/baiteng01/library/developer/coresimulator/devices/92444384-5241-4934- B078-1a7241f1b687/data/containers/data/application/73005382-d1fb-4bc2-bb4e-1fbc64284141/documents/xiaoxi.plist      *     */         //writing data to plist file     nsmutabledictionary *dic1 = [nsmutabledictionary  dictionarywithobjectsandkeys:@ "Little Tiger", @ "name", @ "5", @ "age", @ "boy" @ "Sex",nil];         NSMutableDictionary *dic2 = [NSMutableDictionary  dictionarywithobjectsandkeys:@ "Little" @ "name" @ "6" @ "Age" @ "girl" @ "Sex",nil];         //Save the above 2 dictionary into the large dictionary     nsmutabledictionary *datadic = [ nsmutabledictionary dictionary];    [datadic setobject:dic1 forkey:@ "First grade"];     [datadic setobject:dic2 forkey:@ "Second Grade"];    //write plist inside     [datadic writetofile:filepatch  atomically:yes];        //read the contents of the Plist file      nsmutabledictionary *datadictionary = [[nsmutabledictionary alloc]  Initwithcontentsoffile:filepatch];    nslog (@ "---plist at the beginning of the preservation of content---%@", datadictionary);
  • plist file Contents/delete/modify/Add/write operations

  • Modify the contents of the dictionary, first according to the structure you want to modify the content of the dictionary nsmutabledictionary *dd = [datadictionary objectforkey:@ "first grade"];    [DD setobject:@ "I changed my name, Oh," forkey:@ "name"];    [DD setobject:@ "New content I added" forkey:@ "content"];        [DD removeobjectforkey:@ "age"];        After the modification succeeds, add this dictionary back to the large dictionary [datadictionary setobject:dd forkey:@ "first grade"];    [Datadictionary Writetofile:filepatch Atomically:yes]; NSLog (@ "---plist after the operation of the dictionary content---%@", datadictionary);
  • Delete plist file

  •     //purge plist files, I can go in the way I told you. See if the plist file is cleared locally     nsfilemanager *filemger = [nsfilemanager  defaultmanager];        nsstring *xiaoxipath = [[ Nssearchpathfordirectoriesindomains (Nsdocumentdirectory, nsuserdomainmask, yes) objectAtIndex:0] stringbyappendingpathcomponent:@ "Xiaoxi.plist"];        //if the file path exists     BOOL bRet = [fileMger fileExistsAtPath:xiaoXiPath];         if  (BRet)  {                 NSError *err;                 [filemger removeitematpath:xiaoxipath error: &err];    } 


Write/read/delete of IOS plist file operations

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.