Plist文件存取数据

简介

将经常变的数据放在文件中进行存储,程序启动后从文件中读取最新的数据。如果要变动数据,直接修改数据文件即可,不用修改代码。
一般可以使用属性列表文件存储 NSArray 或者 NSDictionary 之类的数据,这种 “属性列表文件” 的扩展名是 plist,因此也称为 “plist 文件”。
plist 是以 xml 文件形式存储的。

iOS 实现的序列化方式的两种:NSKeyedArchiver,NSPropertyListSerialization。在这两种序列化方式中,NSData 都是序列化的目标。两种方式的不同点在于 NSPropertyListSerialization 是针对数组和字典类型的,而 NSKeyedArchiver 是针对对象的。

write写入方式,永久保存在磁盘中
  • 方法步骤:
    1、获得文件即将保存的路径
 NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//有三个参数:目录类型、domain mask、布尔值。(固定)

2、生成在该路径下的文件

/* fileName 就是保存文件的文件名  */
NSString *FileName = [documentPath stringByAppendingPathComponent:@"fileName"];

3、往文件中写入数据

// 将 NSData 类型对象 data 写入文件,文件名为 FileName
[data writeToFile:FileName atomically:YES];

4、从文件中读出数据

// 从 FileName 中读取出数据
NSData *data = [NSData dataWithContentsOfFile:FileName options:0 error:NULL];

plist文件的读写

    NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayToPList.plist"];
    NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryToPList.plist"];
        
    // 待写入数据1
    NSArray *array = @[@"bei", @"jing", @"huan", @"ying", @"nin"];
    // 待写入数据2
    NSDictionary *dictionary = @{@"name":@"chen chao", @"age":@"18", @"info":@"Good Teacher"};
        
    // 写 Plist 文件
        
        // 数组写入 plist 文件
        BOOL bl1 = [array writeToFile:arrayPath atomically:YES];
        // 字典写入 plist 文件
        BOOL bl2 = [dictionary writeToFile:dictionaryPath atomically:YES];
        
    // 读 Plist 文件
    
        NSArray *arrayFromPlist = [NSArray arrayWithContentsOfFile:arrayPath];
        NSDictionary *dicFromPList = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];

plist序列化

    NSString *arrayPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/arrayPropertyList.plist"];
    NSString *dictionaryPath = [NSHomeDirectory() stringByAppendingString:@"/Desktop/dictionaryPropertyList.plist"];
        
    // 待写入数据
    NSArray *array = @[@"bei", @"jing", @"huan", @"ying", @"nin"];                                                  
    // 待写入数据
    NSDictionary *dictionary = @{@"name":@"chen chao", @"age":@"18", @"info":@"Good Teacher"};                      
        
    // 序列化,将数据转换成 XML 格式的文件
    
        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];
            
        // 输出到 .txt 格式文件中
        BOOL bl1 = [arrayData writeToFile:arrayPath atomically:YES];                                                    
        BOOL bl2 = [dictionaryData writeToFile:dictionaryPath atomically:YES];
        
    // 反序列化
    
        NSArray *arrayFromeFile = [NSArray arrayWithContentsOfFile:arrayPath];
        NSDictionary *dicitionaryFromeFile = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath];

plist文件的使用

1.创建

  • 一般方式
Plist文件存取数据_第1张图片
QQ.png
  • 代码方式

2.解析

    // 获得 Plist 文件的全路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];

    // 加载 plist 文件
    NSArray *shops = [NSArray arrayWithContentsOfFile:path];

注意

plist 的文件名不能叫做 Info.plist , INfo.plist, xxxInfo.plist等
添加 plist 等文件资源的时候,一定要勾选下面的选项。

Plist文件存取数据_第2张图片
QQ.png

代码方式使用plist

创建

// 获取本地沙盒路径
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 获取完整路径
NSString *documentsPath = [path objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"newsTest.plist"];
//创建数据
NSMutableDictionary *newsDict = [NSMutableDictionary dictionary];
//赋值
[newsDict setObject:@"zhangsan" forKey:@"name"];
[newsDict setObject:@"12" forKey:@"age"];
[newsDict setObject:@"man" forKey:@"sex"];
//写入
[newsDict writeToFile:plistPath atomically:YES];

读取

获取路径
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path1 = [pathArray objectAtIndex:0];
NSString *myPath = [path1 stringByAppendingPathComponent:@"newsTest.plist"];
读取数据
//newsModel.plist文件
NSMutableArray *data1 = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
//newsTest.plist文件
NSMutableDictionary *data2 = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

添加数据

//用新建文件的方式常见的plist文件,获取其路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"newsModel" ofType:@"plist"];
//代码方式创建的plist文件获取其路径
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path1 = [pathArray objectAtIndex:0];
NSString *myPath = [path1 stringByAppendingPathComponent:@"newsTest.plist"];
//取出数据
//newsModel.plist文件
NSMutableArray *data1 = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
//newsTest.plist文件
NSMutableDictionary *data2 = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//处理,写入
//新建一个字典,设置属性值
NSMutableDictionary *addData1 = [NSMutableDictionary dictionary];
[addData1 setObject:@"123" forKey:@"title"];
[addData1 setObject:@"pic.png" forKey:@"image"];
[addData1 setObject:@"wobushi" forKey:@"detail"];
//添加到数组中
[data1 addObject:addData1];
//写入文件
[data1 writeToFile:filePath atomically:YES];

//增加一个字段”job“,并设置属性值
[data2 setObject:@"writer" forKey:@"job"];
//写入文件
[data2 writeToFile:plistPath atomically:YES];

你可能感兴趣的:(Plist文件存取数据)