IOS NSSecureCoding

2022-01-28  本文已影响0人  f8d1cf28626a

> 代码

```js

#import "NSString+Paths.h"

@implementation NSString (Paths)

+(NSString*)callBackSavePeripheralsForPath{

    NSString *paths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    NSString *folder = [paths stringByAppendingPathComponent:@"Header"];

    returnfolder;

}

@end

#import

NS_ASSUME_NONNULL_BEGIN

@interface ObjectModel : NSObject<NSSecureCoding>

@property (nonatomic,strong) NSString *peripherals;

@property (nonatomic,strong) NSArray *identifiers;

@end

NS_ASSUME_NONNULL_END

#import "ObjectModel.h"

@implementation ObjectModel

// 遵循<NSSecureCoding>协议

- (void)encodeWithCoder:(NSCoder*)coder {

    /// String

    [coderencodeObject:self.peripherals forKey:@"peripherals"];

    /// Array error

  // [coder encodeObject:self.identifiers forKey:@"identifiers"];

}

- (instancetype)initWithCoder:(NSCoder*)coder {

    self.peripherals = [coder decodeObjectForKey:@"peripherals"];

    ///error

  // self.identifiers = [coder decodeObjectForKey:@"identifiers"];

    return self;

}

+(BOOL)supportsSecureCoding{

    return YES;

}

NSError*error;

    ObjectModel *model = [[ObjectModel alloc] init];

    model.peripherals=@"peripreals";

  // model.identifiers = @[@"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];

    }

    ///归档

    NSMutableArray *models = [[NSMutableArray alloc] init];

    [modelsremoveAllObjects];

    [modelsaddObject:model];

    NSData *encodeData = [NSKeyedArchiver archivedDataWithRootObject:model requiringSecureCoding:YES error:&error];

    NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];

//    [archive encodeObject:[models copy] forKey:@"models"];

//    [archive finishEncoding];

//    NSData *encodeData = archive.encodedData;

    BOOLisWrite = [encodeDatawriteToFile:filePathatomically:YES];

    if(isWrite){

    }

    else{

        NSLog(@"write error");

    }

    ///解档

    error =nil;

    NSData*unarchiveData = [NSDatadataWithContentsOfFile:filePath];

    ObjectModel *unarchiveModel = [NSKeyedUnarchiver unarchivedObjectOfClass:[ObjectModel class] fromData:unarchiveData error:&error];

//    NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingFromData:unarchiveData error:&error];

//    [unarchive decodeObjectOfClass:[ObjectModel class] forKey:@"models"];

//    NSArray *unarchiveModels = [unarchive decodeObjectForKey:@"models"];

//    [unarchive finishDecoding];

    NSLog(@"%@...%@",unarchiveModel.peripherals,error);

    NSLog(@"%@...%@",unarchiveModel.identifiers,error);

```

上一篇 下一篇

猜你喜欢

热点阅读