#import "NSString+Paths.h"
@implementation NSString (Paths)
+(NSString*)callBackSavePeripheralsForPath{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *folder = [paths stringByAppendingPathComponent:@"Header"];
returnfolder;
}
@end
#import "ObjectModel.h"
@implementation ObjectModel
// 遵循<NSSecureCoding>协议
- (void)encodeWithCoder:(nonnullNSCoder*)coder {
[coderencodeObject:_peripherals forKey:@"ps"];
}
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder {
_peripherals = [coder decodeObjectForKey:@"ps"];
return self;
}
-(void)setPeripherals:(NSArray*)peripherals{
_peripherals= peripherals;
}
-(NSArray *)getPeripherals{
return _peripherals;
}
+(BOOL)supportsSecureCoding{
return YES;
}
@end
-(void)viewDidLoad{
[super viewDidLoad];
NSError*error;
ObjectModel *model = [[ObjectModel alloc] init];
model.peripherals= @[@"identifier"];
NSFileManager *fm = [NSFileManager defaultManager];
NSString *folderPath = [NSString callBackSavePeripheralsForPath];
if(![fmfileExistsAtPath:folderPath]){
/// 创建了文件夹 Header
[fmcreateDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
}
NSString *filePath = [folderPath stringByAppendingPathComponent:@"data.peripherals"];
NSLog(@"%@",filePath);
if(![fmfileExistsAtPath:filePath]){
/// 创建了文件 data.peripherals
[fmcreateFileAtPath:filePath contents:nil attributes:nil];
}
NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
[archiveencodeObject:modelforKey:@"model"];
[archivefinishEncoding];
NSData*encodeData = archive.encodedData;
BOOLisWrite = [encodeDatawriteToFile:filePathatomically:YES];
if(isWrite){
}
else{
NSLog(@"write error");
}
}
网友评论