iOS---归档与解档

/*
iOS归档小结:
1.归档实际上就是一种文件保存的形式,几乎所有的对象都能够被归档存储。它使用NSKeyedArchiver进行归档(归档的数据为:二进制数据)、NSKeyedUnarchiver进行解档,它会在写入、读出数据(二进制数据)之前对数据进行序列化、反序列化操作。

 2.另外也需要介绍一下:NScoder 和 NScoding,NScoder 和 NScoding有将自己定义的类的对象写入磁盘的作用,-(id)initWithCoder:(NSCoder )coder;//从coder中读取数据,保存到相应的变量中,即反序列化数据
 -(void)encodeWithCoder:(NSCoder )coder;// 读取实例变量,并把这些数据写到coder中去。序列化数据
 NSCoder 是一个抽象类,抽象类不能被实例话,只能提供一些想让子类继承的方法。
    */

/*
单一对象的归档

NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径
NSString* archivePath = [path stringByAppendingPathComponent:@"archive.plist"];
NSLog(@"%@",archivePath);
NSArray* arrMut =@[@{@"asd":@"12",@"asd1":@"12",@"asd2":@"12"},@{@"asd2":@"12"}];
//归档--序列化
[NSKeyedArchiver archiveRootObject:arrMut toFile:archivePath];

NSArray* unArr = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];//解档---反序列化
NSLog(@"%@",unArr);

*/

/*多个对象的归档*/
NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径
 NSString* archivePath = [path stringByAppendingPathComponent:@"archive.plist"];
NSLog(@"%@",archivePath);
//归档
NSMutableData* data = [[NSMutableData alloc]init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对多个对象归档
[archiver encodeBool:YES forKey:@"isOr"];
[archiver encodeFloat:12.356 forKey:@"floatwe"];
[archiver finishEncoding];
[data writeToFile:archivePath atomically:YES];


//解档
NSMutableData* data1 = [[NSMutableData alloc]initWithContentsOfFile:archivePath];
NSKeyedUnarchiver* unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
BOOL iOS = [unarchive decodeBoolForKey:@"isOr"];
float floatwe = [unarchive decodeFloatForKey:@"floatwe"];
[unarchive finishDecoding];
NSLog(@"%d %f",iOS,float);

-------------------归档和解档结合Model实现-----------------------------

import

@interface Worker : NSObject

@property (nonatomic,strong) NSString* name;//姓名
@property (nonatomic,assign) NSString* age;//年龄
@property (nonatomic,strong) NSString* workType;//职业
@property (nonatomic,assign) NSString* wage;//工资
@property (nonatomic,assign) NSString* workTime;//工作年限

@property (nonatomic,strong) NSDictionary* dict;

  • (instancetype)initWithDict:(NSDictionary *)dic;

@end


import "Worker.h"

@implementation Worker

  • (instancetype)initWithDict:(NSDictionary *)dic{

// self = [super init];
if ( self = [super init]) {

    self.dict = dic;
}
return self;

}

  • (void)setDict:(NSDictionary *)dict{
    self.name = [dict objectForKey:@"name"];
    self.age = [dict objectForKey:@"age"] ;
    self.workType = [dict objectForKey:@"workType"];
    self.wage = [dict objectForKey:@"wage"] ;
    self.workTime = [dict objectForKey:@"workTime"];

}

  • (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.age forKey:@"age"];
    [aCoder encodeObject:self.workType forKey:@"workType"];
    [aCoder encodeObject:self.wage forKey:@"wage"];
    [aCoder encodeObject:self.workTime forKey:@"workTime"];
    }

  • (id)initWithCoder:(NSCoder *)aDecoder{

    self = [super init];
    if (self) {
    self.name = [aDecoder decodeObjectForKey:@"name"];
    self.age = [aDecoder decodeObjectForKey:@"age"];
    self.workType = [aDecoder decodeObjectForKey:@"workType"];
    self.wage = [aDecoder decodeObjectForKey:@"wage"];
    self.workTime = [aDecoder decodeObjectForKey:@"workTime"];
    }
    return self;
    }

@end

具体用法:
NSDictionary* dict = @{@"name":@"zxc",@"age":@"5",@"workType":@"iOS",@"wage":@"50",@"workTime":@"2"};
Worker* work = [[Worker alloc]initWithDict:dict];
NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径

NSString* archivePath = [path stringByAppendingPathComponent:@"worker.plist"];
NSLog(@"%@",archivePath);
if ([NSKeyedArchiver archiveRootObject:work toFile:archivePath]) {
    
    Worker* worker = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
    NSLog(@"%@ %@ %@ %@ %@",worker.age,worker.name,worker.workTime,worker.workType,worker.wage);
}

你可能感兴趣的:(iOS---归档与解档)