zoukankan      html  css  js  c++  java
  • IOS-Archiver文件归档(2)

    Archiver是持久化数据的一种方式,他跟 Plist的差别在于他能持久化自己定义对象。但他没Plist那么方便。

    Archiver默认能持久化的数据有NSNumber,NSArray,NSDictionary,NSString,NSData,由于这几个对象已经实现了

    <NSCoding>协议。如果我们要实现一个对象的Archiver持久化 ,也必须实现该对象。


    1.<NSCoding>协议主要为归档/恢复文件两个方法

    //恢复归档文件为对象
    -(id)initWithCoder:(NSCoder *)aDecoder
    //归档,使对象持久化
    -(void)encodeWithCoder:(NSCoder *)aCoder


    ----------------

    例如以下 。我们首先获取归档文件的路径

    #pragma mark 获取文件路径
    - (NSString *) filePath
    {
        NSArray *dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
        NSString *dirPath=dirPaths[0];
        NSString *filePath=[dirPath stringByAppendingPathComponent:@"aa.archiver"];
        return filePath;
    }


    2.系统默认对象怎样归档(NSNumber,NSArray,NSDictionary,NSString,NSData)

    #pragma mark 归档/恢复 Array对象
    - (void) savearray
    {
        
        NSString *filePath=[self filePath];
    //
    //    NSArray *arr=@[@"ttt",@"BBB",@25];
    //    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
    //
        NSArray *arr1=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"%@",arr1);
    }

    #pragma mark 归档/恢复 Dictionary对象
    - (void) saveDic
    {
        NSString *filePath=[self filePath];
    //    NSDictionary *dict=@{@"name":@"lean",@"age":@25};
    //    BOOL flag=[NSKeyedArchiver archiveRootObject:dict toFile:filePath];
    //    NSLog(@"%d",flag);
        NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"%@",dict2);
    }

    3.怎样归档自己定义对象。

    定义了一个Person类。例如以下:

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject <NSCoding>
    
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,assign) int age;
    
    + (Person *) initWithName:(NSString *)name andAge:(int) age;
    
    @end
    
    #import "Person.h"
    
    @implementation Person
    
    + (Person *) initWithName:(NSString *)name andAge:(int) age
    {
        Person *p=[[Person alloc] init];
        p.name=name;
        p.age=age;
        return p;
    }
    
    -(void)encodeWithCoder:(NSCoder *)aCoder
    {
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeInt:self.age forKey:@"age"];
    }
    
    -(id)initWithCoder:(NSCoder *)aDecoder
    {
        [self setName:[aDecoder decodeObjectForKey:@"name"]];
        [self setAge:[aDecoder decodeIntForKey:@"age"]];
        return self;
    }
    
    @end
    
    TIP: 无论是encode还是decode 都是依据对象的类型去选用不同的方法。如

    encodeInt:forkey:      encodeDouble:forkey:   encodeFloat:forkey: 

    decodeObjectForKey:  decodeIntForKey:  decodeDoubleForKey:


    NSKeyedArchiver archiveRootObject:toFile:

    NSKeyedUnarchiver unarchiveObjectWithFile:  

    各自是对须要归档。

    恢复的对象进行操作的两个类


    定义完了Person类后,在须要归档的地方调用例如以下:

    #pragma mark 归档/恢复 自己定义对象
    - (void) savePerson
    {
        NSString *filePath=[self filePath];
        Person *p=[Person initWithName:@"lean" andAge:22];
        BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
        Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"%d-%d",flag,p2.age);
    }

    对于其Person类,如果该类中还有自己定义对象作为属性。相同实现<NSCoding>协议


    4.如果该对象是某个对象子类,这里我们建立一个叫Student类作为Person的子类


    #import "Person.h"
    
    @interface Student : Person
    
    @property (nonatomic ,assign) int no;
    
    + (Student *) initWithName:(NSString *)name andAge:(int) age andNO:(int) no;
    
    @end

    相同Student也须要实现NSCoding协议的方法

    -(id)initWithCoder:(NSCoder *)aDecoder
    {
        if (self=[super initWithCoder:aDecoder]) {
            [self setNo:[aDecoder decodeIntForKey:@"no"]];
        }
        return self;
    }
    
    -(void)encodeWithCoder:(NSCoder *)aCoder
    {
        [super encodeWithCoder:aCoder];
        [aCoder encodeInt:self.no forKey:@"no"];
    }

    #pragma mark 归档/恢复 自己定义子类对象
    - (void) saveStudent
    {
        NSString *filePath=[self filePath];
        Student *p=[Student initWithName:@"lean" andAge:22 andNO:150133];
        BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
        Student *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSLog(@"%d-%@",flag,p2.name);
    }




  • 相关阅读:
    企业要主动淘汰五种人
    人力管理的核心:选、用、育、留
    张瑞敏:正确路线确定后,干部就是决定因素
    西点军校如何培养学员
    董事长如何找合适的搭档
    企业家何时应该放权
    会计基础视频
    同样劳动,为何结果不同?
    什么是真正的工作到位
    中国的人口和经济周期
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5313015.html
Copyright © 2011-2022 走看看