NSKeyedArchiver 归档储存 (建立文件夹成功之后必须归档到具体文件上不然会失败)

 

注意:建立好文件之后,一定要再加入一个路径后者名字,不然会归档失败.

 

model类型

#import 

NS_ASSUME_NONNULL_BEGIN

@interface Persion : NSObject

@property(nonatomic,strong) NSString *name;
@property(nonatomic,assign) int age;
@property(nonatomic,strong) NSString *sex;

+(Persion *)persionWith:(NSString *)name age:(int)age sex:(NSString *)sex;

@end
#import "Persion.h"

@implementation Persion

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInt:self.age forKey:@"age"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    
    if (self) {
        
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntForKey:@"age"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];

    }
    
    return  self;
}

+(Persion *)persionWith:(NSString *)name age:(int)age sex:(NSString *)sex{
    
    Persion *per = [[Persion alloc]init];
    per.name = name;
    per.age = age;
    per.sex = sex;
    
    return per;
}

-(NSString *)description{
    return [NSString stringWithFormat:@"name %@    age %d    sex %@",self.name,self.age,self.sex];
}



@end

注以下方法实验过归档和反归档


    Persion *p1 = [Persion persionWith:@"aaaaa" age:23 sex:@"nv"];
    Persion *p2 = [Persion persionWith:@"bbbbb" age:33 sex:@"nan"];
    Persion *p3 = [Persion persionWith:@"ccccc" age:28 sex:@"nv"];

    NSArray *arr = @[p1,p2,p3];
    
    
    
    NSString * scourcePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * path1 = [[scourcePath stringByAppendingPathComponent:@"MiDou"] stringByAppendingPathComponent:@"123"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    
    if([NSKeyedArchiver archiveRootObject:arr toFile:[path1 stringByAppendingString:@"midou.text"]]){
        NSLog(@"成功");
    }else{
        NSLog(@"失败");
    }
    //将path文件路径的数据解档出来
    NSArray *unarchiveNumbers = [NSKeyedUnarchiver unarchiveObjectWithFile:path1];

NSKeyedArchiver  归档储存  (建立文件夹成功之后必须归档到具体文件上不然会失败)

***************************************************

model类型


#import 

NS_ASSUME_NONNULL_BEGIN

@interface Persion : NSObject

@property(nonatomic,strong) NSString *name;
@property(nonatomic,assign) int age;
@property(nonatomic,strong) NSString *sex;

+(Persion *)persionWith:(NSString *)name age:(int)age sex:(NSString *)sex;

@end

NS_ASSUME_NONNULL_END
#import "Persion.h"

@implementation Persion

+(BOOL)supportsSecureCoding{
    return YES;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInt:self.age forKey:@"age"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    
    if (self) {
        
        self.name = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"name"];
        self.age = [[aDecoder decodeObjectOfClass:[NSNumber class] forKey:@"age"] intValue];
        self.sex = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"sex"];
    }
    
    return  self;
}

+(Persion *)persionWith:(NSString *)name age:(int)age sex:(NSString *)sex{
    
    Persion *per = [[Persion alloc]init];
    per.name = name;
    per.age = age;
    per.sex = sex;
    
    return per;
}

-(NSString *)description{
    return [NSString stringWithFormat:@"name %@    age %d    sex %@",self.name,self.age,self.sex];
}



@end

本人xcode跟新到10.1了发现上面的方法弃用

NSKeyedArchiver 归档储存 (建立文件夹成功之后必须归档到具体文件上不然会失败)_第1张图片

新的实现方法:

    Persion *p1 = [Persion persionWith:@"aaaaa" age:23 sex:@"nv"];
    Persion *p2 = [Persion persionWith:@"bbbbb" age:33 sex:@"nan"];
    Persion *p3 = [Persion persionWith:@"ccccc" age:28 sex:@"nv"];

    NSArray *arr = @[p1,p2,p3];

    //路径
    NSString * scourcePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * path1 = [[scourcePath stringByAppendingPathComponent:@"MiDou"] stringByAppendingPathComponent:@"123"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
    }

    //归档
     NSData *data1 = [NSKeyedArchiver archivedDataWithRootObject:arr requiringSecureCoding:YES error:nil];
    if ([data1 writeToFile:[path1 stringByAppendingString:@"midou.text"] atomically:YES]) {
        NSLog(@"arr成功");
    } else {
        NSLog(@"arr失败");
    };

    //解档
    NSError *err = nil;
    NSArray *array = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:[NSData dataWithContentsOfFile:[path1 stringByAppendingString:@"midou.text"] options:NSDataReadingMappedIfSafe error:&err] error:&err];
    NSLog(@"array:: %@",array);
    if (err) {
        NSLog(@"err %@",err.description);
    }

解档的时候 单一类型的使用 [Persion  class] , [NSString class] ......之类的  ,而数组里面放复杂类型,并且遵守NSSecureCoding或者NSCoding协议的 使用了[NSObject class]

你可能感兴趣的:(iOS,持久化)