iOS-数据存储方式一之plist文件存储

来源:互联网 发布:淘宝天猫内部优惠券群 编辑:程序博客网 时间:2024/05/22 16:42

plist文件,实际上也就是xml文件。
iOS开发中,对于一些小型的数据保存,可以用到plist文件存储。
plist存储能存NSArray、NSDictionary等,一般能写出writeToFile:这个方法的对象都可以使用plist存储,存放的路径一般在沙盒的Document文件目录下。


plist文件的写入:

    //获取沙盒Document路径    NSString *currentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];    NSString *filePath = [currentPath stringByAppendingPathComponent:@"test.plist"];    NSDictionary *dictionary = @{@"name":@"HZhenF",@"age":@"21"};    //若之前没有该文件,会自动创建该文件    if ([dictionary writeToFile:filePath atomically:YES]) {        NSLog(@"写入成功");    }

plist文件的读取:

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

删除plist文件:

    NSFileManager *manager = [NSFileManager defaultManager];    [manager removeItemAtPath:filePath error:nil];

查看生成的plist文件:

NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));

用这句代码获取沙盒中的Document路径,然后打开finder

这里写图片描述

这里写图片描述

原创粉丝点击