ios NSKeyedUnarchiver在iOS11 以上的用
2023-05-09 本文已影响0人
天上飞的狒狒
0 归档数据的存取
#pragma mark - 数据归档
//存缓存数据
-(void)writeData:(NSArray *)dataArr andFfieldName:(NSString *)pathName {
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSMutableArray *leftArray = [NSMutableArray arrayWithArray:dataArr];
NSString *leftArrPath = [paths stringByAppendingString:pathName ];
[[NSFileManager defaultManager] removeItemAtPath:leftArrPath error:nil];
NSError *errorObj = [NSError new];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:leftArray requiringSecureCoding:NO error:&errorObj];
[data writeToFile:leftArrPath options:NSDataWritingAtomic error:&errorObj];
}
//取缓存数据
-(NSArray *)loadDataArrWithfieldName:(NSString *)pathName{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *leftArrPath = [paths stringByAppendingString:pathName ];
if ([[NSFileManager defaultManager] fileExistsAtPath:leftArrPath]) {
NSData *newData = [NSData dataWithContentsOfFile:leftArrPath];
NSArray *dataArr = [NSKeyedUnarchiver unarchivedObjectOfClasses:[self loadSetArr] fromData:newData error:nil];
return dataArr;
}
return nil;
}
//处理所有的数据类型
-(NSSet *)loadSetArr {
return [NSSet setWithArray:@[NSArray.class,NSDictionary.class, NSString.class, UIFont.class, NSMutableArray.class, NSMutableDictionary.class, NSMutableString.class, UIColor.class, NSMutableData.class, NSData.class, NSNull.class, NSValue.class, NSDate.class, NewsRecommendModel.class, AppRecommendListModel.class]];
}
1 这是传统的自定义model.h文件
@interface NewsRecommendModel : NSObject
@property (nonatomic, copy) NSString *showTopLine;
@property (nonatomic, copy) NSString *showBottomLine;
@end
2 这是传统的自定义model.m文件
#import "NewsRecommendModel.h"
@interface NewsRecommendModel()<NSCoding, NSSecureCoding> //NSCoding老的协议 ,NSSecureCoding新的协议,协议必须遵守
@end
@implementation NewsRecommendModel
// 接归档此方法必须添加(接档此处必须添加)
+ (BOOL)supportsSecureCoding {
return YES;
}
//用于归档数据
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.showTopLine forKey:@"showTopLine"];
[aCoder encodeObject:self.showBottomLine forKey:@"showBottomLine"];
}
//用于 反归档读取数据
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.showTopLine = [aDecoder decodeObjectForKey:@"showTopLine"];
self.showBottomLine = [aDecoder decodeObjectForKey:@"showBottomLine"];
return self;
}
+ (instancetype) newsRecommendModelWithDictionary:(NSDictionary*)dictionary {
NewsRecommendModel *model = [[self alloc] init];
model.showTopLine = @"1";
model.showBottomLine = @"1";
return model;
}
@end
3 这是yymodel处理的model.h文件
#import <Foundation/Foundation.h>
@interface AppRecommendListModel : NSObject
@property (nonatomic, copy) NSString *type;
@property (nonatomic, copy) NSString *isVip;
@property (nonatomic, copy) NSString *siteId;
@property (nonatomic, copy) NSString *linkUrl;
@property (nonatomic, copy) NSString *channelId;
@property (nonatomic, copy) NSString *updateDate;
@property (nonatomic, copy) NSString *regionName;
@property (nonatomic, copy) NSString *channelLogo;
@property (nonatomic, copy) NSString *channelDesc;
@property (nonatomic, copy) NSString *channelName;
@property (nonatomic, copy) NSString *followStatus;
@property (nonatomic, copy) NSString *isClientSubscribe;
@property (nonatomic, copy) NSString *forbidSubNavigation;
@end
4 这是yymodel处理的model.m文件
#import "AppRecommendListModel.h"
@interface AppRecommendListModel()<NSCoding, NSSecureCoding>
@end
@implementation AppRecommendListModel
- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
return YES;
}
//yymodel用于 反归档读取数据
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
return [self yy_modelInitWithCoder:coder];
}
//yymodel用于 归档读取数据
- (void)encodeWithCoder:(NSCoder *)coder
{
[self yy_modelEncodeWithCoder:coder];
}
// 此方法必须添加(接档此处必须添加)
+ (BOOL)supportsSecureCoding {
return YES;
}