iOS 归档和解档

作者: 王蓝胖 | 来源:发表于2016-03-14 01:29 被阅读1587次
  • iOS的几种数据持久化方案

  • plist文件(属性列表)

  • preference(偏好设置)

  • NSKeyedArchiver(归档)

  • SQLite

  • CoreData

  • 沙盒目录结构

  • "应用程序包": 这里面存放的是应用程序的源文件,包括资源文件和可执行文件。

  • Documents: 最常用的目录,iTunes同步该应用时会同步此文件夹中的内容,适合存储重要数据。

  • Library/Caches: iTunes不会同步此文件夹,适合存储体积大,不需要备份的非重要数据。

  • Library/Preferences: iTunes同步该应用时会同步此文件夹中的内容,通常保存应用的设置信息。

  • tmp: iTunes不会同步此文件夹,系统可能在应用没运行时就删除该目录下的文件,所以此目录适合保存应用中的一些临时文件,用完就删除。

  • plist文件,plist文件是将某些特定的类,通过XML文件的方式保存在目录中。

  • 这里先说说自定义类的归档和解档
    NSObject没有遵循<NSCoding>协议,所以想要调用- (instancetype)initWithCoder:(NSCoder *)aDecoder/- (void)encodeWithCoder:(NSCoder *)aCoder方法需要让自定义类遵循该协议
    如果需要归档的类是某个自定义类的子类时,就需要在归档和解档之前先实现父类的归档和解档方法。即 [super encodeWithCoder:aCoder][super initWithCoder:aDecoder]方法;

  • 使用
    需要把对象归档是调用NSKeyedArchiver的工厂方法 archiveRootObject: toFile: 方法。
    需要从文件中解档对象就调用NSKeyedUnarchiver的一个工厂方法 unarchiveObjectWithFile: 即可。

  • 代码

#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCoding>

@property (nonatomic, copy) NSString *dream;

@end
#import "Person.h"

@implementation Person

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        NSLog(@"initWithCoder");
        _dream = [aDecoder decodeObjectForKey:@"DreamKey"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    NSLog(@"encodeWithCoder");
    [aCoder encodeObject:_dream forKey:@"DreamKey"];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end
#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic, strong) Person *boy;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *p = [[Person alloc] init];
    self.boy = p;
    
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [btn1 setBackgroundColor:[UIColor blueColor]];
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 100, 100)];
    [btn2 setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    
    [btn1 setTitle:@"归档" forState:UIControlStateNormal];
    [btn2 setTitle:@"解档" forState:UIControlStateNormal];
    
    [btn1 addTarget:self action:@selector(clickBtn1) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(clickBtn2) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)clickBtn1{
    self.person.dream = @"happy";
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:self.person];
    
    NSString *homePath = NSHomeDirectory();
    NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
    [NSKeyedArchiver archiveRootObject:arr toFile:path];
}

- (void)clickBtn2{
    
    NSString *homePath = NSHomeDirectory();
    NSString *path = [homePath stringByAppendingPathComponent:@"Library/Caches/hehe.archive"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (arr.count) {
        self.person = arr[0];
        NSLog(@"%@", self.person.dream);
    }

}

@end

运行结果

归档和解档
  1. 点击解档,啥都没发生,
  2. 点击归档,打印encodeWithCoder
  3. 退出App
  4. 点击解档,打印"initWithCoder" "happy"

相关文章

  • iOS NSKeyedArchiver数据归档

    iOS中利用NSKeyedArchiver和NSKeyedUnarchiver进行数据的归档和解档操作。 归档 所...

  • iOS. swift 本地数据归档和解档

    iOS. swift 本地数据归档和解档 userDataModel 文件 代码: class MyModel: ...

  • iOS归档和解档

    OC的序列化和反序列化就是用来存储对象和访问对象。序列化就是通过归档把对象转化成二进制文件。反序列化就是通过解档把...

  • iOS归档和解档

    关键词: 归档:数据持久化的一种方式,是将数据进行编码序列化之后存储的过程。适用于小量数据的存储。 解档:对归档的...

  • iOS 归档和解档

    iOS的几种数据持久化方案 plist文件(属性列表) preference(偏好设置) NSKeyedArchi...

  • iOS数据持久化

    记录一下别人总结的,嘿嘿嘿【IOS学习基础】归档和解档

  • iOS之归档和解档

    归档和解档 什么是Serialization? 写数据到本地磁盘或者进行传输时,需要进行序列化,转化成二进制流,从...

  • 数据持久化-归档、解档

    模型对象实现归档和解档代理方法 解档 宏定义路径 归档 任何地方只要动了数组都要归档

  • 归档和解档

    Animal.m ViewController

  • 归档和解档

    一、归档介绍 1.归档是指用某种格式来保存一个或多个对象,以便以后还原这些对象的过程。归档是将数据持久化的一种方式...

网友评论

    本文标题:iOS 归档和解档

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