Introduction to the

The frequently changing data is stored in a file, and the latest data is read from the file after the program is started. If you want to change the data, you can just modify the data file without changing the code. In general, you can use a property list file to store data such as an NSArray or NSDictionary. This property list file has the extension PLIST, so it is also called a “plist file.” Plist is stored as an XML file.

The iOS implementation way of the serialization of two kinds: NSKeyedArchiver, NSPropertyListSerialization. In both serialization modes, NSData is the target of serialization. Two ways of difference from NSPropertyListSerialization is for arrays and dictionaries, and NSKeyedArchiver is in view of the object.

save

Write Indicates the write mode, which is permanently stored on the disk
  • Methods and steps:

1. Obtain the path where the file will be saved

NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // There are three parameters: directory type, domain mask, and Boolean value. (fixed)Copy the code

2. Generate files in this directory

/ * fileName is to save the file fileName * / nsstrings * fileName = [documentPath stringByAppendingPathComponent: @ "fileName"].Copy the code

3. Write data to the file

// Write the NSData object data to a file named FileName [data writeToFile:FileName atomically:YES];Copy the code

4. Read data from files

/ / read out data from FileName NSData * data = [NSData dataWithContentsOfFile: FileName options: 0 error: NULL];Copy the code

Plist file read and write

NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayToPList.plist"]; NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryToPList.plist"]; / / to write data array = 1 NSArray * @ [@ "bei" @ "jing", @ "huan", @, "ying," @ "nin"]. / / to write data dictionary = 2 NSDictionary * @ {@ "name:" @ "Chen chao", @ "age" : @ "18," @ "info" : @ "Good the Teacher"}; BOOL bl1 = [array writeToFile:arrayPath atomically:YES]; / / dictionary written to the file BOOL bl2 = [dictionary writeToFile: dictionaryPath atomically: YES]; / / read file NSArray * arrayFromPlist = [NSArray arrayWithContentsOfFile: arrayPath]; NSDictionary *dicFromPList = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];Copy the code

Plist serialization

NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayPropertyList.plist"]; NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryPropertyList.plist"]; / / to write data NSArray * array = @ [@ "bei" @ "jing" @ "huan", @, "ying," @ "nin"]. / / to write data NSDictionary * dictionary = @ {@ "name:" @ "Chen chao", @ "age" : @ "18," @ "info" : @ "Good the Teacher"}; // Serialize, Transform data into XML format documents NSData * arrayData = [NSPropertyListSerialization dataWithPropertyList: array format:NSPropertyListXMLFormat_v1_0 options:1 error:nil]; NSData *dictionaryData = [NSPropertyListSerialization dataWithPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 options:1 error:nil]; BOOL bl1 = [arrayData writeToFile:arrayPath Atomically :YES]; BOOL bl2 = [dictionaryData writeToFile:dictionaryPath atomically:YES]; / / deserialization NSArray * arrayFromeFile = [NSArray arrayWithContentsOfFile: arrayPath]; NSDictionary *dicitionaryFromeFile = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];Copy the code

The use of plist files

1. Create

  • General way

  • Code way

2.

NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@" Plist "]; NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@" Plist "]; / / load the file NSArray * shops = [NSArray arrayWithContentsOfFile: path];Copy the code

Pay attention to

The file name of plist cannot be info.plist, info.plist, xxxinfo.plist, etc

When adding file resources such as PList, be sure to check the following options.

Use PLIST in code

create

/ / the local sandbox path NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory NSUserDomainMask, YES); NSString *documentsPath = [path objectAtIndex:0]; NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"newsTest.plist"]; NSMutableDictionary *newsDict = [NSMutableDictionary]; NSMutableDictionary *newsDict = [NSMutableDictionary]; // assign [newsDict setObject:@"zhangsan" forKey:@"name"]; [newsDict setObject:@"12" forKey:@"age"]; [newsDict setObject:@"man" forKey:@"sex"]; // write [newsDict writeToFile:plistPath atomically:YES];Copy the code

read

Access path NSArray * pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory NSUserDomainMask, YES); NSString *path1 = [pathArray objectAtIndex:0]; NSString *myPath = [path1 stringByAppendingPathComponent:@"newsTest.plist"]; Read data / / newsModel. File NSMutableArray * data1 = [[NSMutableArray alloc] initWithContentsOfFile: filePath]; / / newsTest. File NSMutableDictionary * data2 = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];Copy the code

Add data

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newsModel" ofType:@"plist"]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newsModel" ofType:@"plist"]; / / code created file for its path NSArray * pathArray = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory NSUserDomainMask, YES); NSString *path1 = [pathArray objectAtIndex:0]; NSString *myPath = [path1 stringByAppendingPathComponent:@"newsTest.plist"]; Take data / / / / newsModel file NSMutableArray * data1 = [[NSMutableArray alloc] initWithContentsOfFile: filePath]; / / newsTest. File NSMutableDictionary * data2 = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath]; NSMutableDictionary *addData1 = [NSMutableDictionary] NSMutableDictionary *addData1 = [NSMutableDictionary]; [addData1 setObject:@"123" forKey:@"title"]; [addData1 setObject:@"pic.png" forKey:@"image"]; [addData1 setObject:@"wobushi" forKey:@"detail"]; // Add to array [data1 addObject:addData1]; [data1 writeToFile:filePath atomically:YES]; [data2 setObject:@"writer" forKey:@"job"]; Data2 writeToFile:plistPath atomically:YES];Copy the code