Plist files are read and written multiple times to implement localization of IOS data and local processing of small amounts of data.

Source: Internet
Author: User

Plist files are read and written multiple times to implement localization of IOS data and local processing of small amounts of data.

Put the following code directly:

 

(Void) viewDidLoad {[super viewDidLoad]; // read the plist file in the Program Folder. Note that the file of the program file can only be read and cannot be written, it can only be in the program sandbox; NSString * plistPath = [[NSBundle mainBundle] pathForResource: @ "plisttest" ofType: @ "plist"]; NSLog (@ "this is the path of the plist file % @", plistPath); NSMutableDictionary * data = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath]; NSLog (@ "this is the content of the plist file % @", data); // Add data instructions, which can be modified; [data setObject: @ "" forKey: @ "address"]; [data setObject: @ "male" forKey: @ "sex"]; // obtain the path of the DOCUMENT in the sandbox; NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * plistPath1 = [paths objectAtIndex: 0]; // set the file name here to complete the path, note that the modification to the file memory is completed in the memory, and the current data is directly updated at a time, which reduces the number of file reads and writes NSString * filename = [plistPath1 stringByAppendingPathComponent: @ "plisttest. plist "]; NSLog (@" this is the path in the sandbox % @ ", filename); // write the file [data writeToFile: filename atomically: YES]; NSLog (@ "prove that my data is actually written in it"); // verify that the file is successfully written to NSMutableDictionary * afterData = [[NSMutableDictionary alloc] initWithContentsOfFile: filename]; NSLog (@ "This is the case where data has been written % @", afterData); NSLog (@ "program ended"); // Do any additional setup after loading the view, typically from a nib .}

The running result is:

 

 

12:08:55. 857 plist [1702: 112934] This is the path of the plist file/Users/xuyaowen/Library/Developer/CoreSimulator/Devices/data/Containers/Bundle/Application/Devices/plist. app/plisttest. plist2015-04-26 12:08:55. 868 plist [1702: 112934] This is the content of the plist file {info = "\ U8fd9 \ U662f \ U6211 \ U7684plist \ U6587 \ U4ef6"; name = xiaohongqi; password = xiaohongqi ;} 12:08:55. 869 plist [1702: 112934] the path in the sandbox/Users/xuyaowen/Library/Developer/CoreSimulator/Devices/6BD2A547-CC45-4739-9578-C1D268C26B20/data/Containers/Data/Application/F145B509-73B0-4266-AE06-94F2B0A56A7C/Documents/plisttest. plist2015-04-26 12:08:55. 902 plist [1702: 112934] proves that my data has to be written into it 12:08:55. 902 plist [1702: 112934] This is the case where data has been written {address = "\ U6211 \ U5728 \ U4e1c \ U5317 \ U5927 \ U5b66 "; info = "\ U8fd9 \ U662f \ U6211 \ U7684plist \ U6587 \ U4ef6"; name = xiaohongqi; password = xiaohongqi; sex = "\ U7537";} 12:08:55. 902 plist [1702: 112934] program ended


 

Then, you can use the log content at the end of the operation to verify the success!

 

Go to the folder: copy the path to access the files in the sandbox. Pay attention to improving your capabilities!

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.