美文网首页
IOS SandBox for Model

IOS SandBox for Model

作者: f8d1cf28626a | 来源:发表于2022-01-27 22:59 被阅读0次

#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");

    }

}

相关文章

网友评论

      本文标题:IOS SandBox for Model

      本文链接:https://www.haomeiwen.com/subject/hakzhrtx.html