zoukankan      html  css  js  c++  java
  • 数据存储值归档Archive

    先比較一下各个数据存储之间的关系:



    关于归档。是ios中的shu'j数据存储中的一种数据存储方式。以下了解一下归档中的一个实例:

    以下的是父类person
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject <NSCoding>
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,assign) int age;
    @property (nonatomic,assign ) BOOL sex;
    @property (nonatomic,assign) float height;
    @property (nonatomic,assign) double weight;
    @end
    
    #import "Person.h"
    
    @implementation Person
    
    //有存必然是有取。所以存是为了取
    - (id)initWithCoder:(NSCoder *)aDecoder{
        
        self = [super init];
        
        if (self) {
            NSLog(@"存在");
            self.name =   [aDecoder decodeObjectForKey:@"name"];
            self.age = [aDecoder decodeInt32ForKey:@"age"];
            self.sex = [aDecoder decodeBoolForKey:@"sex"];
            self.height = [aDecoder decodeFloatForKey:@"height"];
            self.weight = [aDecoder decodeDoubleForKey:@"weight"];
        }
        return self;
    }
    
    -(void)encodeWithCoder:(NSCoder *)aCoder{
    
        //有文件就能够看出编码的是一个方式编码
        [aCoder encodeObject:self.name forKey:@"name"];
        [aCoder encodeInt:self.age forKey:@"age"];
        [aCoder encodeBool:self.sex forKey:@"sex"];
        [aCoder encodeFloat:self.height forKey:@"height"];
        [aCoder encodeDouble:self.weight forKey:@"weight"];
    
    }
    @end
    
    
    
    
    

    子类student继承person类:
    
    #import <Foundation/Foundation.h>
    #include "Person.h"
    
    @interface Student : Person
    
    @property (nonatomic,copy )NSString * content;
    @property (nonatomic,assign ) float grade;
    
    @end
    #import "Student.h"
    
    @implementation Student
    
    //有存必然是有取,所以存是为了取
    - (id)initWithCoder:(NSCoder *)aDecoder{
    
        if (self = [super initWithCoder:aDecoder]) {
            NSLog(@"存在");
            self.content =   [aDecoder decodeObjectForKey:@"content"];
            self.grade = [aDecoder decodeFloatForKey:@"grade"];
                }
        return self;
    }
    
    -(void)encodeWithCoder:(NSCoder *)aCoder{
        
        [super encodeWithCoder:aCoder];
        //有文件就能够看出编码的是一个方式编码
        [aCoder encodeObject:self.content forKey:@"content"];
        [aCoder encodeFloat:self.grade forKey:@"grade"];
    
    }
    
    @end
    

    以下是在ViewController中中对数据通过两button对数据进归档和解归档

    #import "ViewController.h"
    #import "Person.h"
    #import "Student.h"
    
    @interface ViewController ()
    //分别室保存和获取的两个button
    - (IBAction)saveArchive:(id)sender;
    - (IBAction)obtainUnarchive:(id)sender;
    @end
    
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    //保存数据
    - (IBAction)saveArchive:(id)sender {
        
        NSLog(@"開始编码归档而且保存");
        //归档需要要素:1、保存对象 2、保存的文件文件夹 3、保存管理器(归档器)
    
        //获取要保存的对象,对象必需要幼稚等等
    //    Person *person = [[Person alloc]init];
    //    person.age = 18;
    //    person.name = @"linyu";
    //    person.sex = YES; //表示为男的
    //    person.height = 180;
    //    person.weight = 60;
        
        Student *stu = [[Student alloc]init];
        stu.age = 18;
        stu.name = @"linyu";
        stu.sex = YES;
        stu.height = 180;
        stu.weight = 60;
        stu.content = @"I am a student !";
        stu.grade = 98.9;
    
        //获取要保存文件的路径
        NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString * dir = [documentDir stringByAppendingString:@"doc.txt"];
        NSLog(@"%@",dir);
        [NSKeyedArchiver archiveRootObject:stu toFile:dir];
    }
    
    //获取数据
    - (IBAction)obtainUnarchive:(id)sender {
        //获取归档之后的内容、
        NSLog(@"获取内容");
        NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString * dir = [documentDir stringByAppendingString:@"doc.txt"];
        Student *per = [NSKeyedUnarchiver unarchiveObjectWithFile:dir];
        NSLog(@"%@",dir);
        NSLog(@"%@ %d %d %.2f %.2f %.2f %@",per.name,per.age,per.sex,per.height,per.weight,per.grade,per.content);
    }
    @end

    输出的结果:


    总结:

    我们能够知道文件归档:
    1、编号设置(preferrence)NSUserDefault:这个事实上也是通过plist文件来存储的。仅仅只是时里面已经通过封装了以后台上面了。存储在preferrence文件里。


    2、plist文件。通过自己获取文件的路径(而且创建)。将数据存储到里面。这里一般都是在dorectory这个文件夹以下。

    3、归档:归档的数据存储是经过一定的压缩。所以显示的不是明文的存储方式,而且归档是用来存储对象的。

    4、注意要存储的类中遵循NSCoding 协议。


    四种存储数据的方式中上面的两种方式是仅仅能够存储对应的主要的数据类型。——> 产生归档的方式进行存储。

    (能够存储对象)



    假设对象是NSStringNSDictionaryNSArrayNSDataNSNumber等类型,能够直接用NSKeyedArchiver进行归档和恢复
    不是全部的对象都能够直接用这样的方法进行归档,仅仅有遵守了NSCoding协议的对象才干够
    NSCoding协议有2个方法:
    encodeWithCoder:

    每次归档对象时,都会调用这种方法。一般在这种方法里面指定怎样归档对象中的每一个实例变量,能够使用encodeObject:forKey:方法归档实例变量

    initWithCoder:

    每次从文件里恢复(解码)对象时,都会调用这种方法。一般在这种方法里面指定怎样解码文件里的数据为对象的实例变量,能够使用decodeObject:forKey方法解码实例变量



  • 相关阅读:
    LINQ Provider表达式树6
    asp.net Forms 验证No.3
    三种用户验证No.1 asp.net Forms
    LinQ表达式目录2
    将ASP.NET MVC 2.0 部署在IIS6和IIS7上
    LINQ Provider 表达式树 5
    asp.net Forms验证No.2
    LINQ表达式树4
    LINQ表达式树3
    绝对精华win8如何使用,玩转win8看完绝不后悔
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6962268.html
Copyright © 2011-2022 走看看