首页 > 代码库 > 持久化存储---归档一

持久化存储---归档一

一、流程图

技术分享

二、代码

1.模型归档

(1)

@interface Person : NSObject<NSCoding>//归档调用的方法
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeObject:self.address forKey:@"adress"];
    [aCoder encodeObject:self.photo forKey:@"photo"];
}
//反归档调用的方法
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) { 
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
        self.address = [aDecoder decodeObjectForKey:@"adress"];
        self.photo = [aDecoder decodeObjectForKey:@"photo"];
    }
    return self;  
}
+ (NSString *)getTheFilePath:(NSString *)filePath{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:filePath];
    return path;
}

(2)

    //1.归档类名:NSKeyedArchiver,解归档类名:NSKeyedUnarchiver
    //2.归档和解档的类必须遵守<NSCoding>,且必须实现- (void)encodeWithCoder:(NSCoder *)aCoder(归档,字段写入)和- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder(返归档的方法,根据key解出)
    //3.定义归档路径
    //4.调用归档和解档的方法
//---------归档与解档一,存储模型,
 //归档
    //1.存储的路径
    //2.存储后的对象数据
    //3.创建归档工具
    //4.将对象按指定路径归档

    NSString *arcHome = [Person getTheFilePath:@"arcHome.archiver"];

    Person *person = [[Person alloc] init];
    person.name = @"";
    person.age = [@28 integerValue];
    person.address = @"以色列";
    person.photo = [UIImage imageNamed:@"Snip20170215_1"];

  
BOOL isSucces
= [NSKeyedArchiver archiveRootObject:person toFile:arcHome]; if (isSucces) { NSLog(@"%@",arcHome); } //解档 Person *persons = [NSKeyedUnarchiver unarchiveObjectWithFile:arcHome]; NSLog(@"name=%@age=%ldadress=%@photo=%@",persons.name,persons.age,persons.address,persons.photo);

2.数组及字典其它方式归档

  

//---------归档与解档三,存储数组或字典
    //1.存取的数据
    //2.存取的路径
    //3.准备存储数据的对象如NSMutableData
    //4.创建归档,开始归档对象,完成归档
    //5.写入文件中
    Person *students = [Person new];
    students.name = @"zi";
    students.age = [@18 integerValue];
    students.address = @"shanghai";
    students.photo = [UIImage imageNamed:@"1.png"];
    
    NSString *dataPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"dataPath.archiver"];
    
    NSMutableData *data =http://www.mamicode.com/ [NSMutableData data];
    
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:students forKey:@"archiver"];
    [archiver finishEncoding];
    
    BOOL result = [data writeToFile:dataPath atomically:YES];
    if (result) {
        NSLog(@"写入成功");
    }else{
        NSLog(@"写入失败");
    }
    
    //----解档
    //1.准备解档路径,根据存储路径来返回Data
    //2.创建反归档对象
    //3.反归档
    //4.完成反归档
    NSData *studentData =http://www.mamicode.com/ [NSData dataWithContentsOfFile:dataPath];
    NSKeyedUnarchiver *unarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:studentData];
    Person *stu = [unarch decodeObjectForKey:@"archiver"];
    [unarch finishDecoding];
    
    NSLog(@"student-->%@--%ld--%@--%@",stu.name,stu.age,stu.address,stu.photo);

 

持久化存储---归档一