(ext.) Read and write iOS learning plist files

Source: Internet
Author: User

when doing iOS development, often use to the plist file, what is the plist file? Its full name is: Property list, the attribute listing file, which is a file used to store serialized objects. The property list file has a. plist extension, and is often referred to as a plist file. The file is in XML format. plist files are typically used to store user settings or to store bundles of information we create a project to learn how to read and write plist files. 1, create a project Plistdemo project creation can find the corresponding plist file, open as shown: In the editor display similar to the form of a table, you can right-click on the plist, open with source mode, you can see the plist file XML format. 2, create a plist file. Press Command+n shortcut key creation, or file-> new->New file, select the property list under Mac OS x to create the plist file named Plistdemo. Open the Plistdemo file, right-click in the blank, right-click Add row, and add data, after adding a successful data, right-click on this data to see Value type Select Dictionary. Dot plus add this dictionary after the data is added after the key is added value, add the phone number and age after the creation is complete with source code to see the plist file is like this: [CPP] View plaincopy<?xml version="1.0"encoding="UTF-8"?> <! DOCTYPE plist Public"-//apple//dtd PLIST 1.0//en" "HTTP://WWW.APPLE.COM/DTDS/PROPERTYLIST-1.0.DTD"> <plist version="1.0"> <dict> <key>jack</key> <dict> <key>phone_num</ke Y> <string>13801111111</string> <key>age</key> <string> A</string> </dict> <key>tom</key> <dict> <key>phone_num&lt ;/key> <string>13901111111</string> <key>age</key> <string> $</string> </dict> </dict> </plist>3, read the plist file data now the file is created successfully, how to read it, the implementation code is as follows: [CPP] View plaincopy- (void) viewdidload {[Super viewdidload]; //Read PlistNSString*plistpath = [[NSBundle mainbundle] Pathforresource:@"Plistdemo"OfType:@"plist"]; Nsmutabledictionary*data =[[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath]; NSLog (@"%@", data);//print data directly. } Printed results: [CPP] View plaincopy plistdemo[6822: f803] {Jack={ Age= A; "Phone_num"=13801111111;          }; Tom={ Age= $; "Phone_num"=13901111111;      }; This will read the data out. 4, creating and writing plist files in the development process, sometimes you need to save some configuration of the program, or game data and so on. The plist data needs to be written. The written plist file is generated in the corresponding program's sandbox directory. Then read the code of the Plist data, add the code to write the data, [CPP] View plaincopy<strong>-(void) viewdidload {[Super viewdidload]; //Read PlistNSString*plistpath = [[NSBundle mainbundle] Pathforresource:@"Plistdemo"OfType:@"plist"]; Nsmutabledictionary*data =[[Nsmutabledictionary alloc] initwithcontentsoffile:plistpath]; NSLog (@"%@", data); //add a piece of content[Data setobject:@"Add some content"Forkey:@"C_key"]; //get the documents directory for the application sandboxNsarray *paths=Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes); NSString*plistpath1 = [Paths Objectatindex:0]; //get the full file nameNSString *filename=[plistpath1 stringbyappendingpathcomponent:@"test.plist"]; //Input Write[Data writetofile:filename Atomically:yes]; //how does that prove my data is written? Read it and see.Nsmutabledictionary *data1 =[[Nsmutabledictionary alloc] initwithcontentsoffile:filename]; NSLog (@"%@", data1); //additional setup after loading the view, typically from a nib.     }      </strong>after acquiring the plistdemo.plist data that you have created manually, add a piece of content to the data to prove that the input was written. How does it prove that the added content was written? Here is the print Result: Code address: https://Github.com/schelling/ycdemo/tree/master/plistdemo

(ext.) Read and write iOS learning plist files

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.