美文网首页
数据存储-归档解档

数据存储-归档解档

作者: 帅哥_刷哥 | 来源:发表于2016-04-19 22:40 被阅读122次

1.介绍

一种可以把任意对象直接保存到文件的方式。
归档:把对象保存到文件中。
解档:从文件中读取对象。

2.注意:

归档会先清空文件,然后重新写入。

3.归档对象的写法

1.对象要实现 NSCoding 协议

@interface Student : NSObject<NSCoding>

@property(nonatomic,copy)NSString *name;

@property(nonatomic,assign) CGFloat score;

@property(nonatomic,assign) BOOL sex;

@end
//1.基类写法
/**
 *  告诉系统需要归档的属性
 *
 *  @param aCoder 解析器
 */
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeFloat:self.score forKey:@"score"];
    [aCoder encodeBool:self.sex forKey:@"sex"];
}

/**
 *  告诉系统解档出来的数据都需要怎么赋值
 *
 *  @param aDecoder 解析器
 *
 *  @return 对象
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeFloatForKey:@"score"];
        self.sex = [aDecoder decodeBoolForKey:@"sex"];
    }
    return self;
}


//1.子类写法
- (void)encodeWithCoder:(NSCoder *)aCoder{
    //一定要调用一下父类的方法
    [super encodeWithCoder: aCoder];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeFloat:self.score forKey:@"score"];
    [aCoder encodeBool:self.sex forKey:@"sex"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    //一定要调用一下父类的方法
    if (self = [super initWithCoder: aDecoder]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeFloatForKey:@"score"];
        self.sex = [aDecoder decodeBoolForKey:@"sex"];
    }
    return self;
}

4.归档 -- NSKeyedArchiver

//归档一个对象
- (void)save{
    //1.准备对象
    Student *stu = [[Student alloc] init];
    stu.name = @"zhangsan";
    stu.score = 90;
    stu.sex = YES;
    //2.要存储的文件路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.归档
    [NSKeyedArchiver archiveRootObject:stu toFile:filePath];
}

//归档一组对象
- (void)save{
    //1.准备对象
    Student *stu = [[Student alloc] initWithName:@"zhangsan" withScore:90 withSex:YES];
    
    Student *stu1 = [[Student alloc] initWithName:@"lisi" withScore:90 withSex:YES];
    
    NSArray *arr = @[stu,stu1];
    //2.要存储的文件路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.归档
    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
}

5.解档 -- NSKeyedUnarchiver

//解析单一对象
- (void)getData{
    //1.要存储的文件路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //2.解档
    Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

//解档一组对象
- (void)getData{
    //2.要存储的文件路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.解档
    NSArray *stus = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",stus);
}

相关文章

  • iOS 5种数据存储

    1.归档解档 归档 解档 2.NSUserDefaults 时候存储少量基本数据类型,比如Array、Bool、S...

  • 数据存储-归档解档

    1.介绍 2.注意: 3.归档对象的写法 4.归档 -- NSKeyedArchiver 5.解档 -- NSKe...

  • iOS 开发:Runtime(详解七)归档解档

    1、归档解档介绍 归档解档是一种常用的轻量型文件存储方式,在项目中,如果需要将数据模型本地化存储,一般就会用到归档...

  • CoreData的使用(二)

    一、概述数据存储可以使用plist,NSUserDefault,归档解档,sqlite,CoreDataCoreD...

  • Runtime实例运用-归档解档

    解档归档是存储方法之一,我们先来回忆一下都有哪些存储方法。 一.回忆存储方法: XML归档解档 NSUserDef...

  • 数据存储(归档解档,沙河存储)

    数据存储 iOS应用数据存储的常用方式 XML属性列表(plist)归档 存Documents中 Prefere...

  • 归档 & 解档

    1.什么是 归档 和 解档 数据本地存储持久化的一种。归档:对象的序列化,通过某种格式把对象保存成本地文件。解档:...

  • 数据存储之归档解档

    项目有收货地址的管理需要进行数据存储这里我们用归档来进行存储 1.因为有多个收货地址所以存储时为数组的存储 2.首...

  • iOS归档和解档

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

  • runtime 进行归档和解档

    数据本地持久化时, 一般会将模型进行归档, 从本地获取数据时, 需要解档下面使用runtime的方式进行归档解档,...

网友评论

      本文标题:数据存储-归档解档

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