04-归档解档

归档解档步骤:

1、遵循协议:
2、实现编码解码这两个方法
// 编码(对象 -> 二进制):告诉系统,归档时,保存哪些属性
-(void)encodeWithCoder:(NSCoder *)aCoder {}
// 解码(二进制 -> 对象):告诉系统,解档时,初始化那些属性
-(nullable instancetype)initWithCoder:(NSCoder *)aDecoder {

3、调用归档解档方法
// 2. 实现协议的encodeWithCoder:方法,告诉系统保存哪些属性
[NSKeyedArchiver archiveRootObject:person toFile:filePath];
// 2.解档
GEPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

/*********** *********** GEPerson *********** ***********/
//  GEPerson.h
//  归档解档
// 1.遵循协议
// 2.实现编码解码这两个方法

#import 
#import "GEComputer.h"

// 1.遵循协议
@interface GEPerson : NSObject 
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) GEComputer *computer;
@end
//  GEPerson.m
//  归档解档

#import "GEPerson.h"

@implementation GEPerson
// 2.实现编码解码这两个方法
// 编码(对象 -> 二进制):告诉系统,归档时,保存哪些属性
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeInteger:_age forKey:@"age"];
    [aCoder encodeObject:_computer forKey:@"computer"];
}
// 解码(二进制 -> 对象):告诉系统,解档时,初始化那些属性
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _name = [aDecoder decodeObjectForKey:@"name"];
        _age = [aDecoder decodeIntegerForKey:@"age"];
        _computer = [aDecoder decodeObjectForKey:@"computer"];
    }
    return self;
}
@end
/*********** *********** GEComputer *********** ***********/
//  GEComputer.h
//  归档解档

#import 

@interface GEComputer : NSObject
@property (nonatomic,copy) NSString *systemName;
@end
//  GEComputer.m
//  归档解档

#import "GEComputer.h"

@implementation GEComputer
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:_systemName forKey:@"systemName"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        _systemName = [aDecoder decodeObjectForKey:@"systemName"];
    }
    return self;
}
@end
/*********** *********** ViewController *********** ***********/
//  ViewController.m
//  归档解档

#import "ViewController.h"
#import "GEPerson.h"
#import "GEComputer.h"

@interface ViewController ()
@end

@implementation ViewController

/**
 归档:保存自定义的数据类型
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    // 打印沙盒路径
    NSLog(@"沙盒路径:%@",NSHomeDirectory());
    // 归档
    [self saveData];
    // 解档
    [self readData];
}

// 归档
-(void)saveData {
    // 1.创建自定义的对象
    GEComputer *computer = [GEComputer new];
    computer.systemName = @"Mac OS";
    
    GEPerson *person = [GEPerson new];
    person.name = @"Jack";
    person.age = 18;
    person.computer = computer;
    
    // 2.归档
    // 2.1 拼接文件路径
    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documents stringByAppendingPathComponent:@"person.data"];
    
    // 2.2 实现协议的encodeWithCoder:方法,告诉系统保存哪些属性
    [NSKeyedArchiver archiveRootObject:person toFile:filePath];
}

// 解档
-(void)readData {
    // 1.获取文件路径
    NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documents stringByAppendingPathComponent:@"person.data"];
    
    // 2.解档
    GEPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    NSLog(@"解档: 名字 = %@,年龄 = %ld,电脑系统 = %@",person.name,person.age,person.computer.systemName);
}
@end

你可能感兴趣的:(04-归档解档)