iOS12之后:[NSKeyedArchiver archiveRootObject: toFile: ]; 弃用了,以下是新的api
遵循NSCoding协议会解档失败,所以遵循NSSecureCoding协议
1.集成<NSCoding>
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding: YES error:&error];
1)requiringSecureCoding = YES
报错:Error Domain=NSCocoaErrorDomain Code=4866 "The data couldn’t be written because it isn’t in the correct format." UserInfo={NSUnderlyingError=0x2836e4cc0 {Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'PYHomeFunctionModel' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'PYHomeFunctionModel' does not adopt it.}}}
2)requiringSecureCoding = NO之后,归档成功,解档 失败
解档报错:Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'PYHomeFunctionModel' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'PYHomeFunctionModel' does not adopt it.}
2.集成< NSSecureCoding >
requiringSecureCoding = YES
解档、归档都可以
3.解档时:切记把所有的对象类型都加进去
[NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[[PYHomeFunctionModel class],[NSString class], [NSArray class]]] fromData:getData error:&error];
model.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface PYHomeFunctionModel : NSObject<NSSecureCoding>
@property (nonatomic, copy) NSString *empid;
@property (nonatomic, copy) NSString *empname;
/// 归档
/// - Parameter model:
+ (BOOL)storeFunctionListModel:(PYHomeFunctionModel *)model;
/// 解档
+ (PYHomeFunctionModel *)readFunctionListModel;
@end
NS_ASSUME_NONNULL_END
model.m
#import "PYHomeFunctionModel.h"
#define LoginInfoPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"loginInfo.data"]
@implementation PYHomeFunctionModel
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.empid forKey:@"empid"];
[coder encodeObject:self.empname forKey:@"empname"];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
self.empid = [coder decodeObjectForKey:@"empid"];
self.empname = [coder decodeObjectForKey:@"empname"];
}
return self;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
/**
存储功能上下架模型数据
*/
+ (BOOL)storeFunctionListModel:(PYHomeFunctionModel *)model{
NSError *error = nil;
BOOL isSaveSuccess = YES;
NSString *filePath = LoginInfoPath;
// [NSKeyedArchiver archiveRootObject:model toFile:LoginInfoPath];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding:YES error:&error];
if (data == nil || error) {
NSLog(@"归档失败:%@",error);
isSaveSuccess = NO;
} else {
BOOL suc = [data writeToFile:filePath atomically:YES];
if (suc) {
NSLog(@"写入成功");
isSaveSuccess = YES;
} else{
NSLog(@"写入失败");
isSaveSuccess = NO;
}
}
return isSaveSuccess;
}
/**
读取功能上下架模型数据
*/
+ (PYHomeFunctionModel *)readFunctionListModel {
NSError *error;
NSString *filePath = LoginInfoPath;
NSData *getData = [NSData dataWithContentsOfFile:filePath];
if (!getData) {
return nil;
}
PYHomeFunctionModel *model = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[[PYHomeFunctionModel class],[NSString class], [NSArray class]]] fromData:getData error:&error];
if (getData == nil || error) {
NSLog(@"解档失败:%@",error);
}
if (model == nil) {
model = [[PYHomeFunctionModel alloc] init];
model.empid = @"";
model.empname = @"";
}
return model;
}
@end
网友评论