本地数据持久化

沙盒特点:安全性比较高,数据不易丢失,常用于存储用户信息,在开发中做一些数据的缓存功能,通常也将数据存入本地沙盒目录中

获取沙盒路径:

 1、NSArray *array =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *DocumentFilePath = [array lastObject];

2、NSString *sendBoxPath = NSHomeDirectory();

往沙盒里面写入文件

//1.首先要创建一个写入文件的路径,比如我们创建一个student.txt文件,放到沙盒中,首先第一步就是先创建路径之后,在通过方法将student.txt写入到创建的路径中,系统会自动生成文

 //2.创建student.txt文件路径,实际就是在document路径后面拼接一个路径

    //拼接document路径在拼接自定义的路径,系统会自动生成新的路径,拼接路径在代码里就是拼接字符串,拼接字符串的方法就是系统指定的方法

        //参数1.你需要拼接的文件夹名或者文件名(最常用的拼接路径方法)

    NSString *studentFilePath = [DocumentFilePath stringByAppendingPathComponent:@"student.txt"];

    //拼接路径的方法2

    NSString *studentFilePath2 = [DocumentFilePath stringByAppendingPathExtension:@"student.xml"];

     //拼接路径方法3

    NSString *studentFilePath3 = [DocumentFilePath stringByAppendingString:@"student.plist"];

  向文件中写入字符串

 BOOL result =  [nameStr writeToFile:studentFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    if (result) {

        NSLog(@"写入成功");

    } else    {

        NSLog(@"写入失败"); }

向student.xml写入数组

NSString *xmlFilePath = [DocumentFilePath stringByAppendingPathComponent:@"student.xml"];

    NSArray *xmlArray = [NSArray arrayWithObjects:@"内容一",@"内容二",@"内容三", nil];

 BOOL resultXml = [xmlArray writeToFile:xmlFilePath atomically:YES];

      if (resultXml) {

        NSLog(@"resultXml写入数组成功"); 

  }  else{

        NSLog(@"写入数组失败");

    }

将字典写入student.plist文件中

 NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"姓名" forKey:@"name"];

  BOOL dicResult = [dic writeToFile:studentFilePath3 atomically:YES];

    if (dicResult) {

      NSLog(@"字典写入成功");

    }  else  {

        NSLog(@"字典写入失败");

    }

  //取出本地student.plist文件中的数据

    NSMutableDictionary *myDic = [NSMutableDictionary dictionaryWithContentsOfFile:studentFilePath3];

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

 

归档,反归档(存model文件)

  //所谓归档,其实就是将特殊类型,如model类,存入本地文件中

  //所谓反归档,其实就是将model对象从本地文件中取出来

归档第一步:新建model类,

归档第二部:实现NSCoding协议

归档第三步:实现协议方法,对属性进行编码操作

- (void)encodeWithCoder:(NSCoder *)aCoder

{

    //其实编码操作就是对属性进行赋值

    [aCoder encodeObject:self.name forKey:@"name"];

    [aCoder encodeObject:self.gender forKey:@"gender"];

    [aCoder encodeInteger:self.age forKey:@"age"];

 }

归档第四步:实现协议方法,对属性进行解码操作

- (id)initWithCoder:(NSCoder *)aDecoder

{

    //解码相当于取值

    self = [super init];

    if (self) {

        self.name = [aDecoder decodeObjectForKey:@"name"];

        self.gender = [aDecoder decodeObjectForKey:@"gender"];

        self.age = [aDecoder decodeIntegerForKey:@"age"];

        }

    return self;

}

归档第五步:归档,将model对象写入本地文件

Studnet *student = [[Studnet alloc] init];

    student.name = @"姓名";

    student.gender = @"女";

    student.age = 18;

    NSString *wangboFilePath = [DocumentFilePath stringByAppendingPathComponent:@"wangbo.abc"];

    //进行归档操作,就是将model对象存入本地文件中

    [NSKeyedArchiver archiveRootObject:student toFile:wangboFilePath];

       //反归档操作,就是从本地文件中取出model对象

    Studnet *wangbo2 = [NSKeyedUnarchiver unarchiveObjectWithFile:wangboFilePath];

    NSLog(@"name = %@, gender = %@, age = %ld", wangbo2.name, wangbo2.gender, wangbo2.age);

 

 

原文地址:https://www.cnblogs.com/xiaolingling1126/p/5082342.html