文件归档和解归档:
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;//归档
+ (id)unarchiveObjectWithFile:(NSString *)path;//接归档
2、NSKeyedArchiver 归档、解归档(自定义的对象,必须要实现<NSCoding>协议)
- (void)encodeWithCoder:(NSCoder *)aCoder;//归档的协议方法,序列化
- (id)initWithCoder:(NSCoder *)aDecoder;//解归档的协议方法,反序列化
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;//归档
+ (id)unarchiveObjectWithFile:(NSString *)path;//接归档
3、对多个对象进行归档、解归档
- (instancetype)initForWritingWithMutableData:(NSMutableData *)data;//用一个可变的data创建归档对象
- (void)encodeObject:(id)objv forKey:(NSString *)key;//将对象归档
- (void)finishDecoding//完成归档(必须执行)
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile//将数据写进归档文件中
- (instancetype)initForReadingWithData:(NSData *)data;//用一个data创建解归档对象,这个data由文件中获取而来
- (id)decodeObjectForKey:(NSString *)key;//解归档
文件归档和解归档的使用:
一、对内置单个对象进行归档和解归档
1.获取归档文件的路径(归档文件名可以自己随意取名)
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = [documents lastObject]; NSString *archiveFileName = [documentPath stringByAppendingPathComponent:@"array.arc"];
2.准备归档的对象数据
NSArray *array = @[@,@,@,@,@,@];
3.对数据进行归档(根据flag标识进行判断,如果返回的是YES,归档成功;反之,归档失败)
if([NSKeyedArchiver archiveRootObject:array toFile:archiveFileName]) { NSLog(@"归档对象成功"); }
4.对数据进行解归档
NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithFile:archiveFileName];
二、对自定义的单个对象进行归档和解归档(由于自定义对象不具有归档的性质,所以要实现归档和解归档,首先必须实现<NSCoding>协议)
1.自定义一个归档对象类,实现协议
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property (copy,nonatomic)NSString *name;
@property (assign,nonatomic)NSInteger age;
@property (assign,nonatomic)char gender;
-(id)initWithName:(NSString*)name andAge:(NSInteger)age andChar:(char)gender;
@end
Person.m
#import "Person.h"
@implementation Person
-(id)initWithName:(NSString*)name andAge:(NSInteger)age andChar:(char)gender
{
self = [super init];
if(self)
{
_name = [name copy];
_age = age;
_gender = gender;
}
return self;
}
-(NSString*)description
{
return [NSString stringWithFormat:@"name:%@,age:%ld,gender:%c",_name,_age,_gender];
}
#pragma mark -<NSCoding>
//归档的协议方法
-(void)encodeWithCoder:(NSCoder *)aCoder//将归档对象序列化
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeInt:_gender forKey:@"gender"];
}
//解归档的协议方法
-(id)initWithCoder:(NSCoder *)aDecoder //将归档对象反序列化
{
self = [super init];
if(self)
{
_name = [[aDecoder decodeObjectForKey:@"name"] copy];
_age = [aDecoder decodeIntegerForKey:@"age"];
_gender = [aDecoder decodeIntForKey:@"gender"];
}
return self;
}
@end
2.获取归档文件的路径(归档文件名可以自己随意取名)
1 NSString *homePath = NSHomeDirectory(); NSString *documentPath = [homePath stringByAppendingPathComponent:@"documents"]; NSString *arcviFileName = [documentPath stringByAppendingPathComponent:@"person.arc"];
3.创建该类归档对象并将其初始化
Person *person = [[Person alloc]initWithName:@"Tom" andAge: andChar:'M'];
4.将归档对象归档
if([NSKeyedArchiver archiveRootObject:person toFile:arcviFileName])
{
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
5.将归档的对象解归档
Person *person2 = [NSKeyedUnarchiver unarchiveObjectWithFile:arcviFileName];
三、对多个对象进行归档和解归档(前面的两种方式都是针对于单个对象进行归档和解归档,如果需要对多个对象进行归档,它们就无用武之地了,局限性很大,因此,这里介绍一个新的归档方式,多对象归档和解归档)
1.获取的文件路径(归档文件名可以自己随意取名)
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [documents lastObject];
NSString *archiveFileName = [documentPath stringByAppendingPathComponent:@"objects.arc"];
2.准备要归档的多个归档对象数据
NSString *useName = @"admin";
NSArray *scores = @[@,@,@90.5,@];
NSString *adress = @"北京市昌平区东三旗";
3.对归档对象进行归档
//归档多个对象(将多个对象读到data中)
//1.用一个可变的data对象创建归档对象
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; //2.归档多个对象
[archiver encodeObject:useName forKey:@"userName"];
[archiver encodeObject:scores forKey:@"scores"];
[archiver encodeObject:adress forKey:@"adress"]; //3.完成归档(必须要实现的方法,要不然归档失败)
[archiver finishEncoding]; //4.将可变的data写进文件
[data writeToFile:archiveFileName atomically:YES]; 说明:这种归档方式,其实就是先将所有的要归档的对象先存入到一个可变的NSMutableData对象data中,然后通过data创建的归档对象将data的数据归档到归档文件中就是了。
4.将归档对象进行解归档
//解归档多个对象
//1.从文件中读出一个data
NSData *data2 = [NSData dataWithContentsOfFile:archiveFileName];
//2.由data创建解归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data2]; //3.按照key解出多个对象
NSString *userName2 = [unarchiver decodeObjectForKey:@"userName"];
NSArray *scores2 = [unarchiver decodeObjectForKey:@"scores"];
NSString *adress2 = [unarchiver decodeObjectForKey:@"adress"]; NSLog(@"uesrName:%@,scores:%@,adress:%@",userName2,scores2,adress2);
说明:这种解归档方式,其实就是从归档文件先读取data,然后通过data创建解归档对象根据指定的键将多个对象都解归档出来就是了。
演示如下:
-- ::56.563 -一个文件多个归档[:] uesrName:admin,scores:(
,
,
"90.5", ),adress:北京市昌平区东三旗