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}