归档/解档

作者: 纳木错_grace | 来源:发表于2016-07-01 12:05 被阅读122次

一、对系统类进行归档/解档

第一种:对集合类对象进行归档/解档

归档:

 NSArray *arr = @[@"one", @1, @"two", @{@"one":@"hello"}, @[@1, @2]];
        BOOL ret = [NSKeyedArchiver archiveRootObject:arr toFile:@"文件路径/arr.archive"];
        if (ret) {
            NSLog(@"归档成功");
        }else{
            NSLog(@"归档失败");
        }

解档

方法(1)

NSArray *arr1 = [NSKeyedUnarchiver unarchiveObjectWithFile:@"文件路径/arr.archive"];
        NSLog(@"%@", arr1);

方法(2)

  NSData *data = [[NSData alloc] initWithContentsOfFile:@"文件路径/arr.archive"];
        NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        NSLog(@"%@", arr2);

第二种:非集合类进行归档/解档

    NSString *name = @"hello";
    NSInteger age = 10;

归档:

        //创建一个空的二进制数据对象
        NSMutableData *mData = [NSMutableData data];
        
        //创建归档对象
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
        
        //将数据写入归档的对象
        //注意:不同的数据类型用不同的方法
        [archiver encodeObject:name forKey:@"Name"];
        [archiver encodeInteger:age forKey:@"Age"];
        //结束归档
        [archiver finishEncoding];
        
        //生成归档文件
        [mData writeToFile:@"文件路径/test.archive" atomically:YES];
        ```
####解档:       
    NSData *data = [NSData dataWithContentsOfFile:@"文件路径/test.archive"];
    //创建解档对象
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    
    //根据key值解档数据,key值必须要和上面自己设定的key一样
    NSString *str = [unarchiver decodeObjectForKey:@"Name"];
    NSInteger age1 = [unarchiver decodeIntegerForKey:@"Age"];
    //结束解档
    [unarchiver finishDecoding];
    
    NSLog(@"%@ %ld", str, age1);
#二、对自定义的类进行归档/解档
####Car.h文件

import <Foundation/Foundation.h>

import "Engine.h"

//自定义类归档,需要遵守NSCoding协议,并实现协议中方法
@interface Car : NSObject<NSCoding>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger speed;
//如果自定义类里有其他自定义类的属性,该作为属性的自定义类页必须遵守NSCoding协议,并且实现其中的方法
@property (nonatomic, retain) Engine *engine;

@end

####Car.m文件

import "Car.h"

@implementation Car

  • (void)dealloc
    {
    self.name = nil;
    self.engine = nil;
    [super dealloc];
    }

//归档的时候自动调用encodeWithCoder
//自定义类归档,就是针对类中的成员变量分别归档
//如果自定类中包含其他自定义类,其他自定义类也要遵守NSCoding协议

//解档的时候自动调用initWithCoder

  • (instancetype)initWithCoder:(NSCoder *)aDecoder{
    //如果存在继承关系,且父类遵守NSCoding协议,并实现协议中的方法,子类中实现initWithCoder方法时,需要写成self = [super initWithCoder:aDecoder]
    if (self = [super init]) {
    self.name = [aDecoder decodeObjectForKey:@"CarName"];
    // self.engine = [aDecoder decodeObjectForKey:@"Engine"];
    //与上面写法等价
    _engine = [[aDecoder decodeObjectForKey:@"Engine"] retain];
    self.speed = [aDecoder decodeIntegerForKey:@"Speed"];
    }
    return self;
    }

@end

####Engine.h

import <Foundation/Foundation.h>

@interface Engine : NSObject<NSCoding>

@property (nonatomic, copy) NSString *name;

@end

####Engine.m

import "Engine.h"

@implementation Engine

  • (void)dealloc
    {
    self.name = nil;
    [super dealloc];
    }

  • (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"EngineName"];
    }

  • (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
    self.name = [aDecoder decodeObjectForKey:@"EngineName"];
    }
    return self;
    }

@end














































































相关文章

  • ios中Swift的归档与解档

    ios中Swift的归档与解档 归档 解档 init()方法 设置属性

  • 归档解档

    归档 解档

  • iOS Runtime应用之自动归档

    归档 解档 end

  • 归档/解档

    一、对系统类进行归档/解档 第一种:对集合类对象进行归档/解档 归档: 解档 方法(1) 方法(2) 第二种:非集...

  • MJExtension归档失败

    MJExtension归档解档奔溃 ,YYMode 归档解档正常的对比由于作者懒得截图,只说明是在[NNObjce...

  • 归档、解档

    1.创建类 ZFPerson,遵循协议NSCoding @interface ZFPerson : NSObjec...

  • 归档/解档

    1.沙盒路径 注意: 0.提供一个在真机情况下查找到沙盒的办法Xcode -> window ->devices找...

  • 归档 & 解档

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

  • 归档、解档

    我对归档和解档的理解是 归档就是把要保存的内容转成Data之后写入指定的路径中。解档就是把指定路径下的Data转成...

  • iOS 5种数据存储

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

网友评论

    本文标题:归档/解档

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