归档/解档

作者: 纳木错_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

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

        本文标题:归档/解档

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