iOS学习 --- iOS12对象序列化(NSKeyedArchiver/NSKeyedUnarchiver)

Posted iOS开发-上海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS学习 --- iOS12对象序列化(NSKeyedArchiver/NSKeyedUnarchiver)相关的知识,希望对你有一定的参考价值。

NSKeyedArchiver 归档

所谓的归档,就是将数据写到一个文件里面去。一般我们的应用的变量常量之类的数据都是在内存里面的,只要APP关闭,这些数据都会丢失。但是把数据存储到文件里面去,就能将数据保存到本地磁盘里面(目前ios基本就是在沙盒里面操作了),不管是APP关闭还是重启设备,下次启动APP的时候都能够读出来。

NSKeyedUnarchiver解档

  • 所谓解档(别人也叫反归档),就是将数据从文件里面读取出来。在程序里面使用。

iOS12之前

归档

+ (NSData *)archivedDataWithRootObject:(id)rootObject API_DEPRECATED("Use +archivedDataWithRootObject:requiringSecureCoding:error: instead", macosx(10.2,10.14), ios(2.0,12.0), watchos(2.0,5.0), tvos(9.0,12.0));
NSMutableArray *array = [NSMutableArray array];
	for (ServiceModel *model in self.selectedArray) 
		NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
		[array addObject:data];
	
	[kUserDefaults setObject:array forKey:keySelectedArray];
	[kUserDefaults synchronize];

解档

+ (nullable id)unarchiveObjectWithData:(NSData *)data API_DEPRECATED("Use +unarchivedObjectOfClass:fromData:error: instead", macosx(10.2,10.14), ios(2.0,12.0), watchos(2.0,5.0), tvos(9.0,12.0));
NSMutableArray *localArray = [NSMutableArray array];
//获取array	
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:keySelectedArray];
	for (NSData *data in array) 
		//解码
		ServiceModel*model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
		[localArray addObject:model];

iOS12之后

归档

+ (nullable NSData *)archivedDataWithRootObject:(id)object requiringSecureCoding:(BOOL)requiresSecureCoding error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
NSMutableArray *array = [NSMutableArray array];
	for (TCMeServiceModel *model in selectedArray) 
		NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding:YES error:nil];
		[array addObject:data];
	
	[kUserDefaults setObject:array forKey:keySelectedArray];
	[kUserDefaults synchronize];

解档

+ (nullable id)unarchivedObjectOfClasses:(NSSet<Class> *)classes fromData:(NSData *)data error:(NSError **)error API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0));
NSMutableArray *localArray = [NSMutableArray array];
	//获取array
	NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:keySelectedArray];
	for (NSData *data in array) 
		//解码
		NSSet *classSet = [NSSet setWithObjects:[ServiceModel class],[NSString class], nil];
		ServiceModel*model = [NSKeyedUnarchiver unarchivedObjectOfClasses:classSet fromData:data error:nil];
		if (model) 
			[localArray addObject:model];
		
	

model

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN
@interface ServiceModel : NSObject<NSSecureCoding>//NSCopying,NSSecureCoding
/** image */
@property (nonatomic, copy) NSString *image;
/** title */
@property (nonatomic, copy) NSString *title;
/** vc */
@property (nonatomic, copy) NSString *vc;


-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)resultListWithDict:(NSDictionary *)dict;

@end

NS_ASSUME_NONNULL_END
#import "ServiceModel.h"

@implementation ServiceModel

-(instancetype)initWithDict:(NSDictionary *)dict
	self = [super init];
	if (self) 
		[self setValuesForKeysWithDictionary:dict];
	
	return self;


+(instancetype)resultListWithDict:(NSDictionary *)dict	
	return [[self alloc]initWithDict:dict];


+ (BOOL)supportsSecureCoding 
	return YES;


//归档
- (void)encodeWithCoder:(nonnull NSCoder *)aCoder 
	[aCoder encodeObject:_image forKey:@"image"];
	[aCoder encodeObject:_title forKey:@"title"];
	[aCoder encodeObject:_vc forKey:@"vc"];


//解档
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder 
	self = [super init];
	if (self) 
		_image = [aDecoder decodeObjectForKey:@"image"];
		_title = [aDecoder decodeObjectForKey:@"title"];
		_vc = [aDecoder   decodeObjectForKey:@"vc"];
	
	return self;


@end

归档存储的是数组,数组中嵌套的是model对象:

另:归档存储的是数组嵌套模型的时候:

①、model对象中要遵循NSSecureCoding协议;

②、model对象.m类中,要实现+ (BOOL)supportsSecureCoding方法,并返回真。

相关文章

iOS - iOS 12 之后的归档(NSKeyedArchiver)、解档(NSKeyedUnarchiver)用法

​​​​​​​ iOS 12对象序列化(NSKeyedArchiver)

iOS应用存储数据的常用的方式之plist和归档​​​​​​​

以上是关于iOS学习 --- iOS12对象序列化(NSKeyedArchiver/NSKeyedUnarchiver)的主要内容,如果未能解决你的问题,请参考以下文章

java核心学习(十七) IO框架---对象序列化

Java 学习笔记 - IO篇:对象流 ObjectInputStreamObjectOutputStream

Java 学习笔记 - IO篇:对象流 ObjectInputStreamObjectOutputStream

021.12 IO流 ObjectStream对象序列化

对象的序列化与反序列化---IO学习笔记

JavaLearn# (12)IO流文件字节(符)流缓冲字节(符)流数据流对象流序列化