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

数据存储-归档解档

作者: 帅哥_刷哥 | 来源:发表于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);
    }
    

    相关文章

      网友评论

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

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