美文网首页
数据持久化方案二----plist

数据持久化方案二----plist

作者: 雨_田 | 来源:发表于2019-03-04 00:46 被阅读0次

写在前方 笔者会用到之前文章的方法(自己封装下)

一、plist是Xcode的一种资源包,可以用来作为存储工具,是一种很直观的可视化文件。它是按一级级节点组成的,根节点是个Array或者Dictionary。

如: 0.png
二、支持的存储数据类型

NSArray(含NSMutableArray)、NSDictionary(含NSMutableDictionary)、NSData(含NSMutableData)、NSString(含NSMutableString)、NSNumber、NSDate、BOOL。同样,存储的对象全是不可变的。

三、创建方式
1、工程里手动创建(只读的,不能用代码写入),
如:


image.png

2、沙盒中创建(可读可写)
如:


image.png

四、相关代码
(一)、工程里手动创建的plist文件
1、存数据
按照需求,根据数组或者字段手动填入数据。

2、读文件

 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"XLTest" ofType:@"plist”];

3、读数据
<1>、根节点是字典

 NSDictionary *aimDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
 NSLog(@"plist :%@",aimDic);

<2>、根节点是数组

 NSArray *aimArr = [NSArray arrayWithContentsOfFile:plistPath];
 NSLog(@"plist :%@",aimArr);

(二)、在沙盒里代码创建的plist文件
1、创建文件

NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *plistPath = [docPath stringByAppendingPathComponent:@"myFile/test.plist"];
//见"写在前方"
[XLFileManagerTool creatFileWithPath:plistPath];

2、存数据

NSMutableDictionary *rootDic = [NSMutableDictionary new];
rootDic[@"key1"] = @"name";
NSArray *ageArr = @[@1, @2, @YES];
rootDic[@"key2"] = ageArr;
[rootDic writeToFile:plistPath atomically:YES];

3、读文件数据
<1>、根节点是字典

 NSDictionary *aimDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
 NSLog(@"plist :%@",aimDic);

<2>、根节点是数组
NSArray *aimArr = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"plist :%@",aimArr);

4、修改数据
根据节点,按需求,从根节点重写write。
如:

NSArray *aimArr = @[@1, @2];
[aimArr writeToFile:plistPath atomically:YES];

5、删除数据
在对应节点删,然后重写放回节点,write进去
如在根节点清空根字典:

NSDictionary *aimD = @{};
[aimD writeToFile:plistPath atomically:YES];

6、删除plist
按照前文中的文件操作即可

如:

[XLFileManagerTool removeFileOfPath:plistPath];  

相关文章

  • 数据持久化方案二----plist

    写在前方 笔者会用到之前文章的方法(自己封装下) 一、plist是Xcode的一种资源包,可以用来作为存储工具,是...

  • Lesson 0-3 Objective-C basic

    15. 数据持久化 数据持久化方式: plist:属性列表plist 文件的常见操作 NSUserDefaults...

  • 面试技巧攻克-iOS中数据持久化

    一、持久化方案 1、iOS中有哪些持久化方案? (1)NSuserDefault(2)Plist(3)归档(序列化...

  • iOS 归档和解档

    iOS的几种数据持久化方案 plist文件(属性列表) preference(偏好设置) NSKeyedArchi...

  • iOS数据本地持久化方法总结

    在iOS开发中,有很多数据持久化的方案,本文章将介绍以下6种方案: plist文件(序列化)preference(...

  • iOS数据持久化小结

    数据持久化方案一般有5种: preference(偏好设置) plist文件(属性列表) NSKeyedArchi...

  • FMDB的简单使用

    前言 日常的项目中,我们经常涉及到数据持久化的问题,苹果给我们提供的出具持久化方案有属性列表(plist)、归档(...

  • iOS-数据的几种持久化方式

    数据的持久化方式:一、沙盒机制二、plist三、NSUserDefaults四、NSKeydedArchiver归...

  • iOS开发-数据持久化之plist文件

    摘要 通过对plist文件的操作对iOS开发中一些数据进行持久化保存。 iOS数据持久化之一——plist文件 i...

  • iOS开发中数据持久化的总结

    数据持久化链接导航: 沙盒基本机制(sandbox) 数据持久化文件读写(plist 文件)NSUserDefau...

网友评论

      本文标题:数据持久化方案二----plist

      本文链接:https://www.haomeiwen.com/subject/pdyvuqtx.html