数据持久化(归档和反归档操作 ,清除缓存等等)

数据持久化的步骤

// 1.指定前往哪个文件夹
// 2,用字符串接收路径
// 3.拼接文件夹路径
// 4.写入本地或者归档操作
// 注 :如果是复杂对象归档 ,要签订NSCoding方法 .并且实现两个协议方法,放在数组里的复杂对象归档也要签协议

苹果手机为了保证自己数据上的绝对的安全设计了沙盒文件 ,每一个应用程序都配备了自己的沙盒文件 ,每一次运行 ,文件夹的名字就会变成一个没有任何规律的字符串

复杂对象写入到本地 ,主要至我们自己创建的对象写入到本地, 也叫归档和反归档操作

结构体 归档与反归档
创建对象

Student *stu =[[Student alloc] initWithName:@"11" hobby:@"11" sex:@"11" age:70];

1.通过数组获取沙盒地址
 NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

2.用字符串保存沙盒路径
NSString *sandBoxPath =sandBox[0];

3.拼接文件夹路径 ,这个文件的扩展名是任意的
NSString *documentPath =[sandBoxPath stringByAppendingPathComponent:@"学生.avi"];

4.对对象进行归档操作
第一个参数 :要试试归档的对象
第二个参数 :路径

[NSKeyedArchiver archiveRootObject:stu toFile:documentPath];

反归档
Student *newstu =[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];

数组归档

Student *stu =[[Student alloc] initWithName:@"11" hobby:@"11" sex:@"11" age:70];
    Student *stu1 =[[Student alloc] initWithName:@"22" hobby:@"22" sex:@"nan" age:71];
    Student *stu2 =[[Student alloc] initWithName:@"33" hobby:@"33" sex:@"nv" age:30];
    NSMutableArray *arr =[NSMutableArray arrayWithObjects:stu,stu1,stu2, nil];

  NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath =sandBox[0];
    NSString *document=[sandBoxPath stringByAppendingPathComponent:@"xuesheng.avi"];

  // 归档
    [NSKeyedArchiver archiveRootObject:arr toFile:document];
    NSLog(@"%@",document);

  // 反归档

  NSArray *newArr =[NSKeyedUnarchiver unarchiveObjectWithFile:document];

   for (Student *stu in newArr) {
        NSLog(@"%@",stu.name);
    }

NSUserDefaults 一般存放的实效的数据,比如字符串等,他用法和字典类似

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];

    [defaults setObject:@"12345" forKey:@"password"];
    [defaults synchronize];
    NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath =sandBox[0];
    NSLog(@"%@",sandBoxPath);
    NSLog(@"%@", [defaults objectForKey:@"password"]);

通过文件管理者对文件进行操作

  NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath=sandBox[0];
    // 创建一个文件管理者
    NSFileManager *manager =[NSFileManager defaultManager];
    // 给要创建的文件夹拼接一个路径
    NSString *filePath =[sandBoxPath stringByAppendingPathComponent:@"guyu"];
    // 文件夹的名不需要有任何的扩展名
    // 通过manager进行文件夹的创建
    [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
//    NSLog(@"%@",filePath);


  // 向新创建的文件夹里写入一个字符串

    NSString *document =[filePath stringByAppendingPathComponent:@"guyu.txt"];
    NSString *str =@"11";

    [str writeToFile:document atomically:YES encoding:NSUTF8StringEncoding error:nil];

移除文件夹
//    [manager removeItemAtPath:filePath error:nil];
//    NSLog(@"%@",sandBoxPath);

移除缓存文件

NSArray *scache= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES);
    NSString *cachePath = scache[0];
    [manager removeItemAtPath:cachePath error:nil];
    NSLog(@"%@",cachePath);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值