先比较一下各个数据存储之间的关系:
关于归档,是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
输出的结果:
总结:
我们可以知道文件归档:1、编号设置(preferrence)NSUserDefault:这个其实也是通过plist文件来存储的,只不过时里面已经通过封装了以后台上面了。存储在preferrence文件中。
2、plist文件,通过自己获取文件的路径(并且创建),将数据存储到里面,这里一般都是在dorectory这个目录下面。
3、归档:归档的数据存储是经过一定的压缩,所以显示的不是明文的存储方式,并且归档是用来存储对象的。
4、注意要存储的类中遵循NSCoding 协议。
四种存储数据的方式中上面的两种方式是只可以存储相应的基本的数据类型。——> 产生归档的方式进行存储。(可以存储对象)
如果对象是
NSString
、
NSDictionary
、
NSArray
、
NSData
、
NSNumber
等类型,可以直接用
NSKeyedArchiver
进行归档和恢复
不是所有的对象都可以直接用这种方法进行归档,只有遵守了
NSCoding
协议的对象才可以
NSCoding
协议有
2
个方法:
encodeWithCoder
:
每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey:方法归档实例变量
initWithCoder
:
每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量