美文网首页
iOS中常用存储方式

iOS中常用存储方式

作者: ios软件开发学习 | 来源:发表于2017-07-29 16:33 被阅读0次

    今天讲四种,CoreData 这哥么我会专门讲.别急

    Preference(偏好设置)

    NSKeyedArchiver归档(NSCoding)

    SQLite3

    Core Data

    应用沙盒

    每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒

    应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Layer)

    下图 ,layer 为资源包

    图片描述

    模拟器应用沙盒的根路径在: (apple是用户名, 8.0是模拟器版本)

    /Users/apple/Library/Application Support/iPhone Simulator/8.0/Applications

    应用沙盒结构分析

    应用程序包:(上图中的Layer)包含了所有的资源文件和可执行文件

    Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录(很少用,苹果可能不审核通过)

    tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同 步设备时不会备份该目录

    Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据

    Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录

    Plist 的存储

    NSSearchPathForDirectoriesInDomains 注意:返回的是一个数组

    接下来直接上代码

    // 点击存储的调用- (IBAction)save:(id)sender {    // 数据存储,是保存到手机里面,

    // Plist存储,就是把某些对象写成plist文件,之前我们的plist文件描述数组,字典

    // plist存储一般用来存储数组和字典

    // Plist存储是苹果特有,只有苹果才能生成plist

    // plist存储不能存储自定义对象

    NSDictionary *dict = @{@"age":@"18",@"name":@"USER"};

    // 保存应用沙盒(app安装到手机上的文件夹)

    // Caches文件夹

    // 在某个范围内容搜索文件夹的路径

    // directory:获取哪个文件夹

    // domainMask:在哪个范围下获取 NSUserDomainMask:在用户的范围内搜索

    // expandTilde是否展开全路径,YES:展开

    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    NSLog(@"%@",cachePath);

    // 拼接文件路径

    NSString *filePath = [cachePath stringByAppendingPathComponent:@"dict.plist"];

    //    // 获取应用沙盒//    NSString *homePath = NSHomeDirectory();    //    NSLog(@"%@",homePath);

    // File:文件全路径 => 所有文件夹路径 + 文件路径

    [dict writeToFile:filePath atomically:YES];

    }// 点击读取的调用- (IBAction)read:(id)sender {

    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    NSLog(@"%@",cachePath);

    // 拼接文件路径

    NSString *filePath = [cachePath stringByAppendingPathComponent:@"dict.plist"];

    // 存的时候用什么对象存,读取的时候也是用什么对象读取

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"%@",dict);

    }

    偏好设置存储 NSUserDefaults

    直接上代码

    - (IBAction)save:(id)sender {    // 偏好设置NSUserDefaults

    // 底层就是封装了一个字典,利用字典的方式生成plist

    // 好处:不需要关心文件名,快速进行键值对存储

    // name USER

    [[NSUserDefaults standardUserDefaults] setObject:@"USER" forKey:@"name"];    // age 18

    [[NSUserDefaults standardUserDefaults] setInteger:18 forKey:@"age"];

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isOn"];

    }

    - (IBAction)read:(id)sender {

    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];  BOOL ison =  [[NSUserDefaults standardUserDefaults] boolForKey:@"isOn"];

    NSLog(@"%@ %d",name,ison);

    }

    归档

    自定义对象一般使用归档,为什么自定义对象需要归档,plist存储不能存储自定义对象

    如果一个自定义对象需要归档,必须遵守NSCoding,而且实现相应方法

    全选复制放进笔记@implementation ViewController- (IBAction)save:(id)sender {

    // 归档:自定义对象一般使用归档,为什么自定义对象需要归档,plist存储不能存储自定义对象

    Person *person = [[Person alloc] init];

    person.age = 18;

    person.name = @"USER";

    // temp

    NSString *tempPath = NSTemporaryDirectory();

    // 拼接文件名

    NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];

    // 归档     | archive v.存档 n.档案文件

    [NSKeyedArchiver archiveRootObject:person toFile:filePath];    //  -[Person encodeWithCoder:]}

    - (IBAction)read:(id)sender {    // 什么对象存,读取出来就是什么对象

    // temp

    NSString *tempPath = NSTemporaryDirectory();

    // 拼接文件名

    NSString *filePath = [tempPath stringByAppendingPathComponent:@"person.data"];

    // 解档

    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];    // -[Person initWithCoder:]

    NSLog(@"%@ %d",p.name,p.age);

    }

    @end// 如果一个自定义对象需要归档,必须遵守NSCoding,而且实现相应方法@interface Person : NSObject@property (nonatomic, assign) int age;@property (nonatomic, strong) NSString *name;@end@implementation Person// 什么时候调用:只要一个自定义对象归档的时候就会调用// 作用:告诉苹果当前对象的哪些属性需要归档- (void)encodeWithCoder:(NSCoder *)aCoder

    {

    [aCoder encodeObject:_name forKey:@"name"];//encode  vt 编译

    [aCoder encodeInt:_age forKey:@"age"];

    }// 什么时候调用:只要一个自定义对象解档的时候就会调用// 作用:告诉苹果当前对象的哪些属性需要解档// initWithCoder:只要解析一个文件的时候就会调用- (id)initWithCoder:(NSCoder *)aDecoder

    {#warning 什么时候调用[super initWithCoder]

    if (self = [super init]) {        // 注意:需要把解档的属性保存到成员属性

    _name = [aDecoder decodeObjectForKey:@"name"];

    _age = [aDecoder decodeIntForKey:@"age"];

    }    return self;

    }@endimplementation RedView// 解析文件的时候调用// 作用:解析xib,storyboard调用- (id)initWithCoder:(NSCoder *)aDecoder

    {    // 这里必须调用[super initWithCoder:aDecoder],super ->UIView

    // 什么时候调用[super initWithCoder:aDecoder],只要父类遵守了NSCoding协议,就调用[super initWithCoder:aDecoder]

    if (self = [super initWithCoder:aDecoder]) {        NSLog(@"%s",__func__);

    }    return  self;

    }@end

    想学习的小伙伴们可以加裙一起交流哦!626433463

    相关文章

      网友评论

          本文标题:iOS中常用存储方式

          本文链接:https://www.haomeiwen.com/subject/mjlclxtx.html