iOS development common data storage methods are: Nskeyedarchiver, nsuserdefaults, write write mode, SQLite. It is necessary to store data in a file format for simple and straightforward storage and visualization of data. plist files are write-written in iOS development and can be displayed as a list of property lists or in XML format. It is convenient for data management. It is necessary to master the use of plist file data operations.
-
Create a. plist file.
Method:
1. Select Xcode,file-new-file ... ;
2, in the project directory right-click, New File ... ;
3, select Xcode, use shortcut cmd+n key combination;
4, Code creation (next step).
Effect. You can select the "property list" in any of the "Resource" in iOS and OS X to create and name the save. The current name is: Userslist.plist.
-
Add data to the. plist file and display
Click Userslist.plist in the Xcode project to open the content that appears in the Userslist.plist file.
You can add data by right-clicking on the "Add row" in the blanks or "root" line in the point. The General data type (type) can be selected for types such as array, Dictionary, string, and so on. You can also assign values (value) to the added data.
In addition to being displayed as a list of property lists, the Userslist.plist file is selected, and the right-click "Open as"-"Source Code" can be displayed in XML format. The current operation result.
-  
creates a. plist file using code.
//Get local sandbox path
Nsarray *path = Nssearchpathfordirectoriesindomains ( NSDocumentDirectory, Nsuserdomainmask, YES);
//Get full path
NSString *documentspath = [path objectatindex:0];
NSString *plistpath = [Documentspath stringbyappendingpathcomponent:@ "Userslist.plist"];
Nsmutabledictionary *usersdic = [[Nsmutabledictionary alloc] init];
/Set property values
[usersdic setobject:@ "Chan" forkey:@ "name"];
[usersdic setobject:@ "123456" forkey:@ "password"];
//write file
[Usersdic Writetofile:plistpath Atomically:yes];
-  
add modify data action
Add or modify existing data. This file is not an existing Userslist.plist file, but a file that exists inside the sandbox. The userslist.plist content is unchanged. Need to view can be viewed into the simulator sandbox.
//Get an existing full path
NSString *plistpath = [[NSBundle mainbundle] pathforresource:@ "Userslist" OfType : @ "plist"];
Nsmutabledictionary *usersdic = [[Nsmutabledictionary Alloc]initwithcontentsoffile:plistpath];
//Set property values, no data is new, existing data is modified.
[[Usersdic objectforkey:@ "users"] setobject:@ "tease" forkey:@ "name"];
[[Usersdic objectforkey:@ "users"] setobject:@ "dbdbdb" forkey:@ "password"];
//write file
[Usersdic Writetofile:plistpath Atomically:yes];
If you want to use a different file name to create a new plist file from the path
NSString *filename=[plistpath stringbyappendingpathcomponent:@ "Test.plist"];//already have the name file is not new, no new.
If you want a multi-layered structure.