IOS development, plist file, read and write those things

What is an plist file?

  • Plist file, that is, the property list file, the full name is Property List, the extension of this file is.Plist, so it is usually referred to as the plist file. It is a file used to store serialized objects, usually used to store user settings in iOS development, and can also be used to store data that is often used rather than changed in a program. Here’s a look at how to create and read and write plist files.

First, create the plist file

  • 1, in the program, through the creation of new files,
    shortcut keys, command + N,
    or File – > New – > File, select iOS–> Resource–> Property List
IOS development, plist file, read and write those things
creates the plist file

Then you name the file:

IOS development, plist file, read and write those things
plist file naming

There’s one thing to note: the name
when not using Info.plist, INfo.plist, xxxInfo.plist and other forms, otherwise there will be the following situation, because a Info.plist file exists in the system of conflict.

The IOS development, plist file, read and write those things
name contains info

The plist file in the figure above can only be of the NSDictionary type, and when you add data, the following will happen:

Add data to IOS development, plist file, read and write those things

usually, we don’t want this effect, so name it with care. Usually, the plist file we want is the same as in the figure below:

In the Root line, Type can select a dictionary or array.

IOS development, plist file, read and write those things
naming conventions

Click Root in this line, and then add a row by right clicking -&gt, Add Row, or clicking the plus sign behind Root. This line contains three attributes, key, type, and value. Where key is the field property, type is the field type, and value is the value corresponding to the field. The Type also contains 7 types, two of which are Array and Dictionary, and the two are in the form of arrays and can contain many key-value below them.

The other 5 are Boolean, data, string, date, and number. These 5 types of data are all data to be included in array and dictionary.

IOS development, plist file, read and write those things
add data and assign
  • 2, the code to create a plist file to get the local NSArray *path
    sandbox path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [path to obtain the full path of objectAtIndex:0]; NSString * plistPath [documentsPath stringByAppendingPathComponent:@ = “newsTest.plist”]; / / create data to create data NSMutableDictionary *newsDict = [NSMutableDictionary dictionary]; / / [newsDict setObject:@ assignment “Zhangsan” forKey:@ “name; [newsDict setObject:@” 12 “forKey:@” age “]; [newsDict setObject:@” man “forKey:@” sex “]; [newsDict writeToFile:plistPath atomically:YES] write plist file;

Two, read the plist file

  • 1, get with the new file path
    common mode plist file, the following method to obtain the path: NSString *filePath = [[NSBundle mainBundle] pathForResource:@ “newsModel” ofType:@ “plist”]; the following code to create plist files to obtain the path way: NSArray *pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *path1 [pathArray = objectAtIndex:0]; NSString = *myPath [path1 stringByAppendingPathComponent:@ “newsTest.plist”];
  • 2,
    file is what type, with what type of data to receive the //newsModel.plist file NSMutableArray *data1 alloc] = [[NSMutableArray initWithContentsOfFile:filePath]; //newsTest.plist NSMutableDictionary *data2 = “NSMutableDictionary alloc] file initWithContentsOfFile:plistPath]; data output is as follows:
IOS development, plist file, read and write those things
data output

Three, add data

The same is to get the path first, and to pay attention to the way the path is taken as the case may be:

With the new file / / common mode plist file, get the NSString *filePath [[NSBundle mainBundle] pathForResource:@ path = "newsModel" ofType:@ "plist"]; / / the code to create plist files to obtain the path of NSArray *pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString *path1 = [pathArray objectAtIndex:0]; NSString *myPath = [path1 stringByAppendingPathComponent:@ newsTest.plist "];

Then the data is taken:

NSMutableArray *data1 = [[NSMutableArray alloc] //newsModel.plist files initWithContentsOfFile:filePath]; //newsTest.plist *data2 = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath] NSMutableDictionary file;

To manipulate data, add data:

/ / a new dictionary, set attribute value NSMutableDictionary *addData1 = [NSMutableDictionary dictionary]; [addData1 setObject:@ "123" forKey:@ "title"]; [addData1 setObject:@ "pic.png" forKey:@ "image"]; [addData1 setObject:@ "wobushi" forKey:@ "detail"]; / / add to the array in the [data1 addObject:addData1] [data1 writeToFile:filePath atomically:YES]; / / write files;
/ / adding a field "job", and set the attribute value [data2 setObject:@ "writer" forKey:@ "job"]; / / [data2 writeToFile:plistPath atomically:YES] to write the file;

The output of the modified data is as follows:

Add data to IOS development, plist file, read and write those things

The above is the common operation of plist files, of course, you can also modify, delete, and other operations, the method is similar, here is not described.