IOS learning-plist file read/write, iosplist

Source: Internet
Author: User

(Conversion) iOS learning-plist file read/write, iosplist

During iOS development, plist files are often used. What is a plist file? Its full name is Property List, which is an attribute List file used to store serialized objects. The property list file extension is. plist, which is usually called a plist file. The file is in xml format. Plist files are usually used to store user settings. They can also be used to store bundled information. We create a project to learn how to read and write plist files. 1. After creating a project Plistdemo project, you can find the plist file corresponding to the project and open it, as shown in. In the editor, right-click the plist file, open the source code to see the xml format of the plist file. 2. Create a plist file. Press the command + N shortcut key to create, or File-> New File, and select the Property List under Mac OS X to create a plist File named plistdemo. Open the plistdemo file, right-click it in the blank area, right-click Add row to Add data, right-click a piece of data, right-click the data, and select value Type to Dictionary. Click the plus sign to add the Value of the Value after the data under this Dictionary is added with the key. After the mobile phone number and age are added, use the source code to view the plist file as follows: [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 </key> <string> 13801111111 </string> <key> age </key> <string> 22 </string> </dict> <key> tom </key> <dict> <key> phone_num </key> <string> 13901111111 </string> <key> age </key> <string> 36 </ string> </dict> </plist> 3. The data read from the plist file is now successfully created. The implementation code is as follows: [cpp] view plaincopy-(void) viewDidLoad {[super viewDidLoad]; // read plist NSString * 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 = 22; "phone_num" = 13801111111 ;}; tom = {age = 36; "phone_num" = 13901111111 ;};} read the data. 4. Creating and writing plist files sometimes requires saving some program configurations or game data during development. In this case, Plist data needs to be written. The written plist file is generated in the sandbox directory of the corresponding program. Then the code for reading plist data is added to the Code for writing data. [cpp] view plaincopy <strong>-(void) viewDidLoad {[super viewDidLoad]; // read plist NSString * plistPath = [[NSBundle mainBundle] pathForResource: @ "plistdemo" ofType: @ "plist"]; NSMutableDictionary * data = [[NSMutableDictionary alloc] Repository: plistPath]; NSLog (@ "% @", data); // add a content [data setObject: @ "add some content" forKey: @ "c_key"]; // obtain the Documents directory N of the application sandbox SArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * plistPath1 = [paths objectAtIndex: 0]; // obtain the complete file name NSString * filename = [plistPath1 stringByAppendingPathComponent: @ "test. plist "]; // input and write [data writeToFile: filename atomically: YES]; // How can I prove that my data is written? Read it and see NSMutableDictionary * data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: filename]; NSLog (@ "% @", data1); // Do any additional setup after loading the view, typically from a nib .} </strong> obtain the manually created plistdemo. after plist data, add a content after the data to prove that the input is written. How can I prove that the added content has been written? The following is the print result: Code address: https://github.com/schelling/YcDemo/tree/master/PlistDemo

 

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.