Dynamically create Plist files and dynamically create plist files

Source: Internet
Author: User

Dynamically create Plist files and dynamically create plist files
Introduction

Property List, an attribute List file, which is used to store serialized objects. The property list file extension is. PlistTherefore, it is usually called a plist file. The file is in xml format.

 

Write a plist File

In the development process, you sometimes need to save some program configurations or game data. In this case, Plist data needs to be written. The written plist file is generated in the sandbox directory of the corresponding program.

1-(void) triggerStorage 2 {3 // displayLabel. text = textInput. text; 4 5 NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); 6 NSString * path = [paths objectAtIndex: 0]; 7 NSString * filename = [path stringByAppendingPathComponent: @ "test. plist "]; // obtain path 8 9 NSDictionary * dic2 = [NSDictionary dictionaryWithContentsOfFile: filename]; // read data 10 NSLog (@" dic2 is: % @ ", dic2 ); 11 12 // create a dic and write it to the plist file 13 NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "displayLabel. text ", @" IP ", nil]; // write data 14 // or [dic setObject: @" add some content "forKey: @" c_key "]; 15 [dic writeToFile: filename atomically: YES]; 16 17}

 

 

Read files

1-(void) readData {2 NSMutableArray * resultData; 3 // obtain the Documents directory of the application sandbox 4 NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES ); 5 NSString * path = [paths objectAtIndex: 0]; 6 NSLog (@ "path = % @", path ); 7 // get the complete file name 8 NSString * filename = [path stringByAppendingPathComponent: @ "test. plist "]; 9 10 // Read File 11 NSDictionary * dic2 = [NSDictionary dictionaryWithContents OfFile: filename]; 12 NSLog (@ "dic is: % @", dic2); 13 if (dic2 = nil) 14 {15 // 1. create a plist file 16 NSFileManager * fm = [NSFileManager defaultManager]; 17 [fm createFileAtPath: filename contents: nil attributes: nil]; 18} 19 else20 {21 resultData = [dic2 objectForKey: @ "IP"]; 22 if ([dic2 count]> 0) 23 {24 // displayLabel. text = resultData; 25 NSLog (@ "read value: % @", resultData); 26} 27 else28 {29 // displayLabel. text = @""; 30 NSLog (@ "no value is read! "); 31} 32} 33}

 

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.