复杂对象、自定义对象写入沙盒

1、自定义一个Person类,遵守NSCoding协议;

// Person类 .h文件
#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCoding>

@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)NSString *phone;
@property (nonatomic,strong)NSString *sex;

- (instancetype)initWithName:(NSString *)name Phone:(NSString *)phone Sex:(NSString *)sex;

@end

// Person类 .m文件

#import "Person.h"

@implementation Person

//自定义初始化方法
- (instancetype)initWithName:(NSString *)name Phone:(NSString *)phone Sex:(NSString *)sex{
    self = [super init];
    if (self) {
        _name = name;
        _phone = phone;
        _sex = sex;
    }
    return self;
}
//对属性进行存储编码
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
}
//读取沙盒属性调用
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.name = [aDecoder decodeObjectForKey:@"phone"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
    }
    return self;
}

2、在控制器导入 person类

- (void)viewDidLoad {
    [super viewDidLoad];
    
//   初始化两个对象
    Person *p1 = [[Person alloc]initWithName:@"张三" Phone:@"110" Sex:@"男"];
    Person *p2 = [[Person alloc]initWithName:@"李四" Phone:@"120" Sex:@"女"];
    
#pragma mark - 归档
//    准备一个空间
    NSMutableData *mutbleData = [NSMutableData data];
//    初始化归档工具,并将归档对象存储在mutableData里
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mutbleData];
//    归档对象
    [archiver encodeObject:p1 forKey:@"onePerson"];
    [archiver encodeObject:p2 forKey:@"twoPerson"];
//    结束归档
    [archiver finishEncoding];
    
    
//    归档后对象写入沙盒
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    path = [path stringByAppendingPathComponent:@"person"];
    [mutbleData writeToFile:path atomically:YES];
    
    NSLog(@"%@",path);
    
#pragma mark - 反归档
    
//    获取路径
    NSString *path1 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    path1 = [path1 stringByAppendingPathComponent:@"person"];
//    获取路径文件的内容
    NSData *data = [NSData dataWithContentsOfFile:path1];
//    通过反归档吧取出来的二进制对象 转换成复杂对象
    NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//    取出person
    Person *per1 = [unArchiver decodeObjectForKey:@"onePerson"];
    Person *per2 = [unArchiver decodeObjectForKey:@"twoPerson"];
//    结束归档
    [unArchiver finishDecoding];
    
//    检验是否成功
    NSLog(@"%@,%@,%@",per1.name,per1.phone,per1.sex);
}

3、多个复杂对象写入文件,采用统一对容器进行一次归档即可(前提是所有复杂对象都遵守NSCoding,对属性都进行编码)

#pragma mark - 多个复杂对象写入沙盒
    NSArray *array = @[p1,p2];

    NSMutableData *personData = [NSMutableData data];
    NSKeyedArchiver *archiver1 = [[NSKeyedArchiver alloc]initForWritingWithMutableData:personData];
    
    [archiver1 encodeObject:array forKey:@"array"];
    [archiver1 finishEncoding];
    
    NSString *path2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    path2 = [path2 stringByAppendingPathComponent:@"array"];
    
    [personData writeToFile:path2 atomically:YES];


你可能感兴趣的:(归档,反归档,复杂对象写入沙盒)