数据持久化

这里写图片描述

    程序运行会分配一个沙盒文件,沙盒文件的文件名每次运行都会变化,文件夹名只一次有效,沙盒一般会保存程序一些信息,缓存等数据,还可以进行病毒的测试.
    沙盒里有三个文件夹 Documents,libary,tmp.
    Documents文件夹:保存用户想要保存的一些信息,比如收藏的内容,设置信息.
    Library文件夹:用来保存开发人员存储的一些东西.
    caches缓存文件夹:用来对缓存信息进行存储,清除缓存就是删除该文件夹.
    Preferences用来保存一些信息,NSUserDefaults创建的plist文件保存在这个文件里.
    tmp临时文件夹:存储临时文件.
    NSBundle指的是当前工程的文件夹,在里面和可以获取图片等的信息,应用程序会在编译的时候把文件变成只读文件,存入到NSBundle里.
一.把简单的对象存入本地.
// 简单对象指:NSString,NSArray....

ViewController.m
- (void)viewDidLoad {
1.把字符串写入到本地.
NSString *str = @"想象一下,在一个装满了平整细沙的盒子里,我们可以尽情随意地在上面作画、涂写,无论画的好坏,最后轻轻一抹,沙盒又回到了原来的平整状态.沙盒的魅力就在于他允许你出错,还可以给你改正的机会.";
// ①.先找到目标文件夹的路径Documents
// 参数1:前往指定文件夹的名,NSDocumentDirectory是documents用的,64行,注意不要和63行混用.还可以指定缓存等文件夹
// 参数2:文件夹的类型,用户类型
// 参数3:YES是绝对路径,能找到整个路径,NO是相对路径,只保留了前往的文件夹的名,前面用~代替.
NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
//    NSLog(@"%@", sandBox);
// ②.寻找路径.
NSString *sandBoxPath = sandBox[0];
// 把要保存的内容进行路径的拼接
NSString *docPath = [sandBoxPath stringByAppendingString:@"/老毛.plist"];
// 把字符串写入到本地.
// 参数1:写入的文件路径.
// 参数2:是否保护正在写入的文件.
// 参数3:编码格式.
[str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", docPath);
    // 从本地读字符串.
    NSString *str = [NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];

这里写图片描述

2.把数组对象写入到本地.
    NSArray *arr = @[@"小黑", @"小红", @"小白", @"小黄"];
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    // 路径拼接.
    NSString *arrPath = [sandBoxPath stringByAppendingPathComponent:@"小韩.plist"];
    // 按照指定的文件路径,数据写入到本地.
    [arr writeToFile:arrPath atomically:YES];
    NSLog(@"%@", arrPath);

    // 从本地把数据读出来.
    NSArray *receiveArr = [NSArray arrayWithContentsOfFile:arrPath];
    for (NSString *str in receiveArr) {
        NSLog(@"%@", str);
    }

     1.指定文件保存的沙盒文件路径.
     2.拼接要保存的文件路径.
     3.根据路径,把数据写入.
     4.按照文件路径,从本地在读取数据.

这里写图片描述

3.把字典写入到本地.
    NSDictionary *dic = @{@"1":@"2", @"3":@"4", @"5":@"6"};
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *dicPath = [sandBoxPath stringByAppendingPathComponent:@"kaka.MP4"];
    [dic writeToFile:dicPath atomically:YES];
    NSLog(@"%@", dicPath);

    NSDictionary *receiveDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@", receiveDic);

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:@"xiaohan" forKey:@"userName"];
    NSLog(@"%@", NSHomeDirectory());
}

这里写图片描述

Student.h
#import <Foundation/Foundation.h>
// 让复杂类写入到本地执行归档和反归档的操作,只有类签订NSCoding协议之后,实现协议方法才能进行归档和反归档的操作.
@interface Student : NSObject<NSCopying>

@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *hobby;
@property(nonatomic, assign)NSInteger age;
@property(nonatomic, retain)NSNumber *number;

- (instancetype)initWithName:(NSString *)name hobby:(NSString *)hobby
         age:(NSInteger)age
      number:(NSNumber *)number;
@end
Student.m
#import "Student.h"

@implementation Student

- (instancetype)initWithName:(NSString *)name hobby:(NSString *)hobby
                         age:(NSInteger)age
                      number:(NSNumber *)number {
    self = [super init];
    if (self) {
        self.age = age;
        self.name = name;
        self.hobby = hobby;
        self.number = number;
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    // 对数据进行编码操作
    // 把需要归档的数据进行编码操作.不需要的就不写了.
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.hobby forKey:@"hobby"];
    [aCoder encodeObject:self.number forKey:@"number"];
    [aCoder encodeInteger:self.age forKey:@"age"];

}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        // 解码的操作
        // 把解码之后的值在赋值给属性.
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.hobby = [aDecoder decodeObjectForKey:@"hobby"];
        self.number = [aDecoder decodeObjectForKey:@"number"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;    
}
@end
4.把复杂对象,写入到本地.
    Student *stu = [[Student alloc] init];
    stu.name = @"小黑";
    stu.hobby = @"小红";
    stu.number = @100;
    stu.age = 30;

    // 归档操作.
    // 找沙盒路径.
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"小黄.MP4"];
    NSLog(@"%@",docPath);
    [NSKeyedArchiver archiveRootObject:stu toFile:docPath];

    // 反归档.
    Student *tempStu = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    NSLog(@"%@, %@", tempStu.name, tempStu.hobby);
//     把复杂对象放到数组里进行归档和反归档的操作
    Student *stu1 = [[Student alloc] initWithName:@"小红" hobby:@"男" age:20 number:@100];
    Student *stu2 = [[Student alloc] initWithName:@"小黄" hobby:@"女" age:21 number:@200];
    Student *stu3 = [[Student alloc] initWithName:@"小白" hobby:@"女" age:22 number:@300];
    Student *stu4 = [[Student alloc] initWithName:@"小黑" hobby:@"男" age:23 number:@400];
    NSArray *arr = @[stu1, stu2, stu3, stu4];
    for (Student *aaa in arr) {
        NSLog(@"%@, %@, %ld, %@", aaa.name, aaa.hobby, aaa.age, aaa.number);
    }
    // 把复杂对象保存到数组中,然后整体进行归档和反归档的操作
    // NSArray因为本身就签署了NSCoding协议,所以可以直接进行归档和反归档操作,自己写的复杂类也必须签NSCoding协议,实现方法,否则就会崩溃.

这里写图片描述

将plist文件,进行归档与反归档.
    // 1.找路径
    NSArray *sandBox1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath1 = sandBox1[0];
    // 2.拼接路径.
    NSString *docPath1 = [sandBoxPath1 stringByAppendingPathComponent:@"student.plist"];
    // 3.归档或者是写入.
    [NSKeyedArchiver archiveRootObject:arr toFile:docPath1];
    NSLog(@"%@", docPath1);

    // 反归档.
    NSArray *temp = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    for (Student *stu in temp) {
        NSLog(@"%@", stu.name);
    }

这里写图片描述

5.文件管理
    // 1.找到文件路径
    NSArray *sandBox2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath2 = sandBox2[0];

    // 创建一个文件管理者.
    NSFileManager *manager = [NSFileManager defaultManager];
    // 要拼接一个文件夹的路径.
    NSString *docPath2 = [sandBoxPath2 stringByAppendingPathComponent:@"ything"];

    // 根据路径创建一个文件夹.
    [manager createDirectoryAtPath:docPath2 withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"********%@", docPath2);

这里写图片描述

6.删除文件夹,文件.
    BOOL result = [manager removeItemAtPath:docPath2 error:nil];
    if (result) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值