标签:
关于归档,是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中中对数据通过两按钮对数据进归档和解归档
#import "ViewController.h" #import "Person.h" #import "Student.h" @interface ViewController () //分别室保存和获取的两个按钮 - (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
总结:
我们可以知道文件归档:3、归档:归档的数据存储是经过一定的压缩,所以显示的不是明文的存储方式,并且归档是用来存储对象的。
4、注意要存储的类中遵循NSCoding 协议。
四种存储数据的方式中上面的两种方式是只可以存储相应的基本的数据类型。——> 产生归档的方式进行存储。(可以存储对象)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u012496940/article/details/47751439