6.16 数据持久化

  • 1、什么是应用程序沙盒?如何访问沙盒?在iOS开发中有何作用?

iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被称为沙盒。所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。

1,每个应用程序都有自己的存储空间;
2,每个应用程序都有自己的存储空间应用程序不能翻过自己的围墙去访问别的存储空间的内容;
3,sandbox是一种安全体系,应用程序的所有操作都要通过这个体系来执行,其中核心内容是:sandbox对应用程序执行各种操作的权限限制。假如不符合条件的话,不会被放行。iOS沙盒为程序运行提供了很好的安全保障

#pragma mark - 沙盒
    // 1. 获取主路径
    NSString *homeString = NSHomeDirectory();
    NSLog(@"%@", homeString);
    // 2. 获取路径
    // 2.1 获取document路径数组
    // 参数1:代表需要查找的文件夹 参数2:文件所在区域 参数3:YES(完整路径),NO(相对路径)
    NSArray *pathArray =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // 2.2 获取document路径
    NSString *docPath = [pathArray firstObject];
    // 2.3 获取lib路径
    NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    // 2.4 获取temp路径(所保存的文件,在iphone重启之后自动删除)
    NSString *tempPath = NSTemporaryDirectory();
  • 2、有哪些常见的数据持久化方式?

数据持久化方式,基本上有以下四种:
属性列表、对象归档、SQLite3和Core Data

#pragma mark - 保存文件名为 1.txt
    // 1. 获取文件的完整路径
    NSString *txtPath = [docPath stringByAppendingPathComponent:@"1.txt"];
    NSLog(@"%@", txtPath);
    // 2. 写入到文件
    [self writeTxtWithPath:txtPath];
    // 3. 测试读取数据
    [self readTxtWithPath:txtPath];

#pragma mark - 保存文件名为 dic.plist
    // 1. 获取完整路径
    NSString *plistPath = [libPath stringByAppendingPathComponent:@"dic.plist"];
    // 2. 写入到plist文件
    [self writePlistWithPath:plistPath];
    // 3. 测试读取文件
    [self readPlistWithPath:plistPath];

#pragma mark - 保存文件名为 image.png

    // 1. 获取完整路径
    NSString *imagePath = [tempPath stringByAppendingPathComponent:@"image.png"];
    // 2. 写入文件
    [self writeImageWithPath:imagePath];

NSUserDefaults,用于存储配置信息
SQLite,用于存储查询需求较多的数据
CoreData,用于规划应用中的对象
使用基本对象类型定制的个性化缓存方案

  • 3、如何使用NSUserDefaults,可以存放哪些数据?
#pragma mark - NSFileManager
    // 1. 创建单例
    NSFileManager *sharedFM = [NSFileManager defaultManager];
    // 2. 进行路径判断
    if ([sharedFM fileExistsAtPath:imagePath]) {
        // 存在的情况
        NSLog(@"该文件存在!");
    } else {
        // 不存在的情况
        NSLog(@"该文件不存在!");
    }
  • 4、plist文件如何创建?如何读取plist文件中的内容?

#pragma mark - 写入txt文件
- (void)writeTxtWithPath:(NSString *)path {

    // 2.1 创建写入文件的内容
    NSString *string = @"小学生喜欢打lol";
    // 2.2 写入文件
    BOOL success =
    [string writeToFile:path
             atomically:YES
               encoding:NSUTF8StringEncoding
                  error:nil];
    // 2.3 判断写入是否成功(断言)
    NSAssert(success, @"写入文件不成功!");


}
#pragma mark - 写入plist文件
- (void)writePlistWithPath:(NSString *)path {
    // 2.1 创建写入文件的内容
    NSDictionary *dic = @{@"name": @"zhangsan",
                          @"age" : @"18",
                          @"sex" : @"male"};
    // 2.2 写入文件
    BOOL success = [dic writeToFile:path atomically:YES];
    // 2.3 判断写入是否成功(断言)
    NSAssert(success, @"写入文件不成功!");

}
#pragma mark - 写入图片文件
- (void)writeImageWithPath:(NSString *)path {
    // 2.1 创建源文件
    UIImage *image = [UIImage imageNamed:@"1"];
    // 2.2 转换成数据流
    NSData *data = UIImagePNGRepresentation(image);
    // 2.3 写入文件
    [data writeToFile:path atomically:YES];

}


#pragma mark - 读取txt文件
- (void)readTxtWithPath:(NSString *)path {
    // 读取文件内容
    NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 打印文件内容
    NSLog(@"string = %@", string);

}
#pragma mark - 读取plist文件
- (void)readPlistWithPath:(NSString *)path {
    // 如果发现读取的数据为null,则直接进入当前路径查看相应的文件
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", dic);

}
#pragma mark - 读取图片文件
- (void)readImageWithPath:(NSString *)path {

}
  • 5、NSFileManager文件管理类如何使用?1)如何向沙盒中存放一个文件?2)如何读取沙盒中的文件
#pragma mark - 自定义对象归档
    // 1. 创建数据源字典
    NSDictionary *personDic = @{@"name": @"zhangsan",
                                @"sex" : @"male",
                                @"code": @"110"};

    // 2. 创建自定义对象
    Person *person = [[Person alloc] initWithDic:personDic];
    // 3. 打印
    NSLog(@"person: %@", person);

    // 4. 压缩
    NSData *data =
    [NSKeyedArchiver archivedDataWithRootObject:person];

    // 5. 创建系统单例
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    // 5.1 存储数据
    [userDefaults setObject:data forKey:@"person"];

    // 6. 解压
    Person *newPerson = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"newPerson:%@", newPerson);

}
  • 6、了解NSCoding协议,实现对象归档

要使用对象归档,对象必须实现NSCoding协议。大部分Object C对象都符合NSCoding协议,也可以在自定义对象中实NSCoding协议,要实现NSCoding协议,实现两个方法:

> - (void) encodeWithCoder:(NSCoder *)encoder 与 -(void)initWithCoder:(NSCoder *)encoder 
> 同时,建议对象也同时实现NSCopying协议,该协议允许复制对象,要实现NSCopying协议须实现
> -(id)copyWithZone:(NSZone *)zone

7,Core data

Core Data本质上是使用SQLite保存数据,但是它不需要编写任何SQL语句。要使用Core Data,需要在Xcode中的数据模型编辑器中设计好各个实体以及定义好他们的属性和关系。之后,通过操作这些对象,结合Core Data完成数据的持久化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值