数据持久化

前奏

    文件的路径有几种方式获取, 比如直接从工程拽出来的路径是当前工程文件夹的路径, 这个路径会随着工程的移动而发生变化
     也可通过 NSBundle 获取, bundle 可以获取工程里文件的路径, 这个路径不会随着工程的移动而变化
     如果要做手机的缓存, 或者收藏功能, 上面两个路径是不能办到的, 我们需要把较为私密的内容放到手机的沙盒里进行保存
    NSLog(@"%@", NSHomeDirectory());

     在沙盒文件里一般有三个大的文件夹
     Documents 文件夹: 用于存储用户的一些信息, 比如收藏的内容, 设置信息. 做收藏功能时, 往这个文件夹里放
     Library 文件夹: 这个文件夹一般是开发者使用, 保存程序员要保存的内容
     Caches 文件夹:Library 里的文件夹, 一般放缓存的信息, 比如网络加载的图片, 清除缓存功能就是删除这个文件夹里的内容
     Preferences 文件夹是设置文件夹, NSUserDefauls 这个类的内容保存在这个文件夹里
     tmp文件夹: 存储临时文件

简单对象的写入

NSString

// 简单对象写入到本地: 指字典, 数组, 字符串等
    NSString *str = @"事实上大大";
    // 1.找沙盒路径
    // 参数1: 指定要去的文件夹, 这个枚举是电脑和手机通用的类, 如果要去 Documents 文件夹则NSDocumentDirectory, 去 Caches 文件夹, 则NSCachesDirectory
    // 参数2: 文件夹类型, 第一个是 用户类型
    // 参数3: 绝对路径(YES), 这个路径是给系统用的, 通过这个路径可以找到对应的文件夹. 相对路径(NO), 这个路径给开发者用的, 通过这个路径能知道要前往的文件夹
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", sandBox);
    NSString *path = [sandBox lastObject];
    NSLog(@"%@", path);

    // 拼接一个用来显示文本内容的文件路径
    // 第一种
    NSString *docPath = [path stringByAppendingString:@"/文本.txt"];
    NSLog(@"%@", docPath);
    // 第二种
    NSString *docPath1 = [path stringByAppendingPathComponent:@"文本.txt"];
    NSLog(@"%@", docPath1);
    // 把字符串写入
    [str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    // 读取本地字符串内容
    NSString *tempStr = [NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", tempStr);

NSArray, NSDictionary

// 数组写入本地
    NSArray *arr = @[@"1", @"2", @"3", @"4"];
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [sandBox lastObject];
    NSLog(@"%@", path);
    // 拼接文件路径
    NSString *docPath = [path stringByAppendingPathComponent:@"arr.plist"];
    // 把数组对象写入到本地
    [arr writeToFile:docPath atomically:YES];

    // 1. 找沙盒路径
    // 2. 拼接文件的路径
    // 3. 写入
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:docPath];
    NSLog(@"%@", tempArr);

复杂对象的写入

// Student类必须签订NSCoding协议
// 数据持久化必须实现的方法
- (void)encodeWithCoder:(NSCoder *)aCoder {
    // 在这个协议方法里需要对属性进行编码操作
    [aCoder encodeObject: self.name forKey:@"name"];
    [aCoder encodeObject: self.sex forKey:@"sex"];
    [aCoder encodeInteger:self.age forKey:@"age"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}
    Student *stu1 = [[Student alloc] init];
    stu1.name = @"Haha";
    stu1.sex = @"man";
    stu1.age = 16;
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [sandBox lastObject];
    NSString *docPath = [path stringByAppendingPathComponent:@"stu.plist"];
    // 写入, 归档, 序列化
    [NSKeyedArchiver archiveRootObject: stu1 toFile:docPath];
    NSLog(@"%@", docPath);

    // 读取, 反归档, 反序列化
    Student *backStu = [NSKeyedUnarchiver unarchiveObjectWithFile: docPath];
    NSLog(@"%@", backStu.name);

数组中保存复杂对象

    Student *stu2 = [[Student alloc] init];
    stu2.name = @"Xixi";
    stu2.sex = @"woman";
    stu2.age = 889;

    Student *stu3 = [[Student alloc] init];
    stu3.name = @"Popo";
    stu3.sex = @"girl";
    stu3.age = 55;

    NSArray *stuArr = @[stu1, stu2, stu3];
    // 数组保存复杂对象: 直接对数组进行归档操作
    NSString *path1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath1 = [path1 stringByAppendingPathComponent: @"StuArr.plist"];
    [NSKeyedArchiver archiveRootObject: stuArr toFile:docPath1];
    NSArray *stuUnArchive = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath1];

    [stuUnArchive enumerateObjectsUsingBlock:^(Student * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@", obj.name);
    }];

文件管理

// 文件管理
    // 创建一个文件管理器
    NSFileManager *manager = [NSFileManager defaultManager];
    // 拼接一个文件夹路径
    NSString *filePath = [sandBoxPath stringByAppendingPathComponent:@"1001"];
    // 创建一个文件夹
    [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];

    NSString *writeStr = @"wo le ge qu";
    NSString *docWriteStr = [filePath stringByAppendingPathComponent:@"writeTxt.txt"];
    [writeStr writeToFile: docWriteStr atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSString *xxStr = [NSString stringWithContentsOfFile:docWriteStr encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", xxStr);

    NSLog(@"%d", [manager removeItemAtPath:docWriteStr error:nil]);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值