iOS开发-数据存储

缓存的存储: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