博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发-数据存储
阅读量:6943 次
发布时间:2019-06-27

本文共 3663 字,大约阅读时间需要 12 分钟。

缓存的存储:NSSearchPathForDirectoriesInDomains
保存:
//这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    //在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录    NSString *cachePath = array[0];    //拼接文件路径,stringByAppendingPathComponent会在cachePath路径后面加上/再拼接    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];    //1.用字典写, plist文件当中保存的是字典.    NSDictionary *dict = @{
@"age" : @18,@"name" : @"gaowei"}; //ToFile:要写入的沙盒路径 [dict writeToFile:filePathName atomically:YES]; //2.用数组写,plist文件当中保存的类型是数组 NSArray *dataArray = @[@56,@"asdfa"]; //ToFile:要写入的沙盒路径 [dataArray writeToFile:filePathName atomically:YES];

读取:

//这个方法获取出的结果是一个数组.因为有可以搜索到多个路径.    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    //在这里,我们指定搜索的是Cache目录,所以结果只有一个,取出Cache目录    NSString *cachePath = array[0];    //拼接文件路径    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];    //从文件当中读取字典, 保存的plist文件就是一个字典,这里直接填写plist文件所存的路径    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePathName];    //如果保存的是一个数组.那就通过数组从文件当中加载.    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
偏好设置的存储:NSUserDefaults
保存:
//偏好设置NSUserDefaults    //底层就是封闭了一个字典,利用字典的方式生成plist文件    //好处:不需要关心文件名(它会自动生成)快速进行键值对存储.    NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];    [defautls setObject:@"gaowei" forKey:@"name"];    [defautls setBool:YES forKey:@"isBool"];    [defautls setInteger:5 forKey:@"num"];    //同步,立即写入文件.    [defautls synchronize];

读取:

//存是用什么key存的, 读的时候就要用什么key值取    //存的时候使用的什么类型,取的时候也要用什么类型.    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];    BOOL isBool  = [[NSUserDefaults standardUserDefaults] boolForKey:@"isBool"];    NSInteger num = [[NSUserDefaults standardUserDefaults] integerForKey:@"num"];    NSLog(@"name =%@-isBool=%d-num=%ld",str,isBool,num);

归档的存储:NSKeyedArchiver

与pist存储,偏好设置存储不同的是,归档可用于存储自定义的类,而那俩者只能存储系统的类的数据
//获取沙盒的路径 person.data 随便写   NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];   NSString *filePathName =  [path stringByAppendingPathComponent:@"person.data"];//    Person *per = [[Person alloc] init];//    per.name = @"xmg";//    per.age = 10;    Student *stu = [[Student alloc] init];    stu.name = @"gxq";    stu.age = 10;    stu.height = 150;    Dog *dog = [[Dog alloc] init];    dog.name = @"wangcai";    stu.dog = dog;    //归档    //archiveRootObject执行这个方法时, 底层会调用要存的对象的encodeWithCoder方法,    //调用encodeWithCoder目的就是想询问下要存对象的哪些属性.怎么存.    [NSKeyedArchiver archiveRootObject:stu toFile:filePathName];

读取:

//获取沙盒的路径    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    NSString *filePathName =  [path stringByAppendingPathComponent:@"person.data"];    //解档    //unarchiveObjectWithFile执行这个方法时,底层会调用要存的对象的initWithCoder方法,    //调用initWithCoder目的就是想询问下要取对象的哪些属性.    Student *per = [NSKeyedUnarchiver unarchiveObjectWithFile:filePathName];
//注意点: 不管是自定义的类对象"per”,继承的类对象”stu”,包含的类对象”dog”,只要归档的时候,都要实现encodeWithCoder,解档就要实现initWithCoder//必须得要遵守NSCoding协议才行//当解析一个文件时就会调用.-(instancetype)initWithCoder:(NSCoder *)decoder{    if (self = [super init]) {       self.name = [decoder decodeObjectForKey:@"name"];       self.age =  [decoder decodeIntForKey:@"age"];    }    return self;}
//当对象写入文件时候调用 -(void)encodeWithCoder:(NSCoder *)encoder{    [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeInt:self.age forKey:@"age"]; }
 

 

 

转载于:https://www.cnblogs.com/liugengqun/p/5168917.html

你可能感兴趣的文章
制作自己的MVC框架(三)——应用
查看>>
青云指南
查看>>
thinkphp 3.2 多表查询 group
查看>>
C#.NET 无法直接启动带有类库输出类型的项目怎么办
查看>>
LeetCode——Best Time to Buy and Sell Stock
查看>>
http连接
查看>>
jquery操作select(取值,设置选中)(转)
查看>>
.gitignore 使用入门
查看>>
性能优化系列八:MYSQL的配置优化
查看>>
jbpm5.1介绍(8)
查看>>
MATLAB概率统计函数(3)
查看>>
关于Haxe3新特性“内联构造方法”的解释
查看>>
第K顺序统计量
查看>>
CSS3之渐变Gradient
查看>>
为什么寄存器比内存快?
查看>>
挖一口井最好的时间除了十年前就是现在
查看>>
C# 文件操作类
查看>>
Google App Engine
查看>>
只需轻松四句话,5分评分轻而易举
查看>>
POJ2528Mayor's posters[线段树 离散化]
查看>>