ISO:数据持久化1(plist)

1、数据持久化的几种方式:

  • plist(XML属性列表)
  • Preference(偏好设置)
  • NSKeyedArchiver归档(NSCoding)
  • SQLite
  • Core Data

2、plist

介绍:plist 文件就是 Property List 文件,我们在 Xcode 中新建一个 Project 的时候,在 Project 中可以看到有一个 Info.plist 文件,这种以 .plist 后缀结尾的文件是以 key-value 键值对的形式保存数据的。在开发中,我们可以使用这些 plist 文件保存一些系统配置、用户信息等的信息

NSString *homePath = NSHomeDirectory();

NSLog(@"homepath = %@", homePath);

两句代码获取沙盒路径:homePath = /Users/xxx/Library/Developer/CoreSimulator/Devices/xxxx/data/Containers/Data/Application/xxxx

 

2.1    命令行查看目录结构

命令1:cd [homePath]

命令2:ls -al 显示目录内容

输出结果:

drwxr-xr-x   7 bull  staff   224  6 15 19:03 .

drwxr-xr-x  41 bull  staff  1312  6 20 10:48 ..

-rw-r--r--   1 bull  staff   209  6 15 19:03 .com.apple.mobile_container_manager.metadata.plist

drwxr-xr-x   3 bull  staff    96  6 20 10:48 Documents

drwxr-xr-x   4 bull  staff   128  6 15 19:03 Library

drwxr-xr-x   2 bull  staff    64  6 15 19:03 SystemData

drwxr-xr-x   2 bull  staff    64  6 15 19:03 tmp

2.2    Finder 搜索

进入finder,commond + shift + G,弹出前往文件夹,

ISO:数据持久化1(plist)

将homePath帖进去机会出现下面界面

ISO:数据持久化1(plist)

 

3、各目录的获取

Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,应用通讯录之类的。
Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据。比如微信朋友圈缓存的照片。
Library/Preference:保存应用的偏好设置,iOS的Settings应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录。比如设置的字体、皮肤、账户信息等。
tmp:保存应用运行时的临时数据,使用完毕后会将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录。

3.1    获取Documents,Caches,Preference,tmp路径

示例代码:

-(void) doPlistTest{

    //获取沙盒路径

    NSString *homePath = NSHomeDirectory();

    NSLog(@"homepath = %@", homePath);

    //获取文件路径,方式1

    NSString *docPath = [homePath stringByAppendingString:@"Documents"];

    NSLog(@"docPath = %@", docPath);

 

    //获取文件路径,方式2

    //调用1,没有api级别限制(属性调用)

    NSString *docPath1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;//调用1

    NSLog(@"docPath1 = %@", docPath1);

    //调用2,有api级别限制(方法调用)[API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0)]

    NSString *docPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];//调用2

    NSLog(@"docPath2 = %@", docPath2);

    

    //获取Caches路径

    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"cachesPath = %@", cachesPath);

    

    //获取tmp路径

    NSString *tmpPath = NSTemporaryDirectory();

    NSLog(@"tmpPath = %@", tmpPath);

    

    //获取Preferences,偏好设置,虽然可以通过拼接路径的方式得到,但是apple不希望这样做,这里使用偏好设置

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

}

4 、plist的使用

4.1、xcode创建一个plist文件的步骤

4.1.1    新建一个 Property List 文件(以下简称 plist 文件),随便起个名字 students.plist

ISO:数据持久化1(plist)

4.1.2   

打开 students.plist 文件,发现它可以存储 Array(数组)和 Dictionary(字典)数据

ISO:数据持久化1(plist)

4.1.3    

由于students列表是个数组,所以我们在 Root 这个 key 中的 Type 选择 Array,并往里面添数据:id、age、name

ISO:数据持久化1(plist)

 

ISO:数据持久化1(plist)

4.1.4

开始添加item的属性的时候可能不太好操作,按照步骤来

(1)把item的Type选择为Dictionary,然后item前面会出现一个的三角标横向往右

(2)点击三角标,会改变方向向下,然后点击item后面的加号就能添加属性了

其实 plist 文件本质就是一个 xml 文件,通过别的sublime打开可以看内容

ISO:数据持久化1(plist)

4.1.5

写段代码读取该文件,并打印

-(void) doPlistSomething {

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *filePath = [bundle pathForResource:@"students" ofType:@"plist"];

    self.students = [NSArray arrayWithContentsOfFile:filePath];

    NSLog(@"学生列表 = %@", self.students);

}

上面代码可以在ViewController.m的- (void)viewDidLoad函数中调用,结果如下,name没有

ISO:数据持久化1(plist)

 

5、指定目录存取plist文件操作

5.1     plist操作array

5.1.1    plist文件写入操作

通过Documents路径手动拼接path保存plist文件

//

-(void) doPlistWrite{

    // 1.创建写入数据,数组

    NSArray *nameArray = @[@"张三",@"李四",@"王五",@"赵六"];

    

    // 2.获取Documents路径,沙盒/Documents

    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"documents %@",documents);//打印路径

    

    // 3.拼接文件路径  沙盒/Documents/array.plist

    NSString *filePath = [documents stringByAppendingPathComponent:@"name.plist"];

    NSLog(@"filePath %@",filePath);//打印路径

    // 4.写入文件

    BOOL success = [nameArray writeToFile:filePath atomically:YES];

    //atomically:线性安全的

}

写入数据后我们通过命令行进入对应的目录打开name.plist文件

ISO:数据持久化1(plist)

5.1.2    plist文件读取操作

通过Documents路径手动拼接path读取指定目录plist文件

-(void) doPlistRead{

    //1.获取documents路径

    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //2.拼接文件路径

    NSString *filePath = [documents stringByAppendingPathComponent:@"name.plist"];

    

    //3.读取数组

    NSArray *nameArray = [NSArray arrayWithContentsOfFile:filePath];

    NSLog(@"name = %@",nameArray);

}

读取结果如下

ISO:数据持久化1(plist)

 

5.2     plist操作NSDictionary

将name_age.plist写入沙盒/Documents/name_age.plist

-(void) doPlistWriteDictionary{

    // 1.创建写入数据,字典

    NSDictionary *nameDictionary = @{@"张三":@15,@"李四":@16,@"王五":@15,@"赵六":@17};//注意数据字典的type是用{}括起来的,类似json

    

    // 2.获取Documents路径,沙盒/Documents

    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"documents %@",documents);//打印路径

    

    // 3.拼接文件路径  沙盒/Documents/array.plist

    NSString *filePath = [documents stringByAppendingPathComponent:@"name_age.plist"];

    NSLog(@"filePath %@",filePath);//打印路径

    // 4.写入文件

    BOOL success = [nameDictionary writeToFile:filePath atomically:YES];

    //atomically:线性安全的

}

ISO:数据持久化1(plist)

 

将 沙盒/Documents/name_age.plist 读取出来

//通过Documents路径手动拼接path读取指定目录plist文件保存NSDictionary

-(void) doPlistReadDictionary{

    //1.获取documents路径

    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    //2.拼接文件路径

    NSString *filePath = [documents stringByAppendingPathComponent:@"name_age.plist"];

    

    //3.读取数组

    NSDictionary *name_age = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"name = %@",name_age);

}

ISO:数据持久化1(plist)

转载于:https://my.oschina.net/goboy/blog/1832835