本文介绍: 就是将数据写到一个文件里面去。一般我们的应用的变量常量之类的数据都是在内存里面的,只要APP关闭,这些数据都会丢失。但是把数据存储到文件里面去,就能将数据保存到本地磁盘里面(目前iOS基本就是在沙盒里面操作了),不管是APP关闭还是重启设备,下次启动APP的时候都能够读出来。②、model对象.m类中,要实现+ (BOOL)supportsSecureCoding方法,并返回真。NSKeyedUnarchiver解档。NSKeyedArchiver 归档。
所谓的归档,就是将数据写到一个文件里面去。一般我们的应用的变量常量之类的数据都是在内存里面的,只要APP关闭,这些数据都会丢失。但是把数据存储到文件里面去,就能将数据保存到本地磁盘里面(目前iOS基本就是在沙盒里面操作了),不管是APP关闭还是重启设备,下次启动APP的时候都能够读出来。
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对象中要遵循NSSecureCoding协议;
②、model对象.m类中,要实现+ (BOOL)supportsSecureCoding方法,并返回真。
相关文章
iOS – iOS 12 之后的归档(NSKeyedArchiver)、解档(NSKeyedUnarchiver)用法
iOS 12对象序列化(NSKeyedArchiver)
iOS应用存储数据的常用的方式之plist和归档
原文地址:https://blog.csdn.net/jiaxin_1105/article/details/125601997
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_15255.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。