Plist first see this suffix file when it feels strange, but contact for a long time also become accustomed to, Plist is the name of the property list can be understood as attribute lists file, mainly used to store the serialized object of the file. The extension is . plist , so called plist file, Xcode is the default is a tree-like structure to show the data, visual dynamic additions and deletions, very user-friendly, but the final result is stored in XML form, The plist file can be used to store some of the user's setup information, depending on the requirements.
Simple File Creation
Simply create a file that means you can create it directly from Xcode, right-click the project new file and add a plist file:
Create a userdata.plist file with the following content:
Right-click Open As->source code, as follows:
<?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>Login</key> <dict> <key>username </key> <string>FlyElephant</string> <key>UserPassWord</key> < string>123456</string> </dict> </dict></plist>
Read the settings information:
Read the property list file nsstring *userdatapath = [[NSBundle mainbundle] pathforresource:@ "UserData" oftype:@ "plist"]; nsmutabledictionary *data = [[Nsmutabledictionary alloc] initwithcontentsoffile:userdatapath]; NSLog (@ "%@", data); NSLog (@ "User name:%@ password:%@", data[@ "login"][@ "UserName"],data[@ "Login"][@ "UserPassword"]); [Data setobject:@ "Login information" forkey:@ "login"];
Change and delete
File Add, above is the application to add files, this time you can choose to add code in the sandbox, the code is as follows:
Nsarray *sandboxpath= nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES); Get the full path nsstring *documentsdirectory = [Sandboxpath objectatindex:0]; NSString *plistpath = [documentsdirectory stringbyappendingpathcomponent:@ "Sandbox.plist"]; Store root data nsmutabledictionary *rootdic = [[Nsmutabledictionary alloc] init]; The detailed data in the dictionary nsmutabledictionary *userdatadic = [[Nsmutabledictionary alloc]init]; [Userdatadic setobject:@ "Flephant" forkey:@ "UserName"]; [Userdatadic setobject:@ "http://www.cnblogs.com/xiaofeixiang/" forkey:@ "UserPassword"]; [Rootdic setobject:userdatadic forkey:@ "Login"]; Write file [rootdic writetofile:plistpath atomically:yes]; NSLog (@ "%@", Nshomedirectory ()); NSLog (@ "write success");
The path is as follows, and the specific path to get the previous article is already visible:
Read data:
Get path nsarray *sandboxpath= nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES); NSString *filepath = [[Sandboxpath objectatindex:0] stringbyappendingpathcomponent:@ "sandbox.plist"]; NSLog (@ "%@", Nshomedirectory ()); Get Data nsmutabledictionary *searchdata = [[Nsmutabledictionary alloc] initwithcontentsoffile:filepath]; NSLog (@ "%@", searchdata);
To modify a file:
Get path nsstring *filepath = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) objectatindex:0]stringbyappendingpathcomponent:@ "Sandbox.plist"]; All data list nsmutabledictionary *datalist= [[[Nsmutabledictionary Alloc]initwithcontentsoffile:filepath] Mutablecopy]; Get the login node nsmutabledictionary *logindata = [DataList objectforkey:@ "Login"]; [Logindata SetValue: @ "flyelephant" forkey:@ "UserName"]; [Logindata SetValue: @ "123456" forkey:@ "UserPassword"]; [DataList setvalue:logindata forkey:@ "Login"]; [DataList Writetofile:filepath Atomically:yes]; NSLog (@ "modified successfully");
To delete a file:
Nsfilemanager *manager=[nsfilemanager Defaultmanager]; File path nsstring *filepath = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) objectatindex:0]stringbyappendingpathcomponent:@ "Sandbox.plist"]; if ([Manager Removeitematpath:filepath Error:nil]) { NSLog (@ "file deletion succeeded"); }
iOS Development-plist file additions and deletions