Objective-c中对象的归档

 
 

   对象的归档是指将对象写入文件保存在硬盘上,当再次挡开程序的时候,可以还原这些对象。对象的归档也叫对象的序列化或对象的持久化

数据的持久性保存的方式:

      1、对象归档NSKeyedArchiver

       2、NSUserDefaults

      3、属性列表化(NSArray,NSDictionary)

      4、SQLite数据库、Core Data数据库

归档的形式:

    1、对Foundation库中对象进行归档    2、自定义对象归档,自定义对象进行归档需要实现归档协议NSCoding

1、下面来实现第一种形式的归档,实现数组的序列化与反序列化

归档

       //实现一个数组的序列化与反序列化
        NSString *homeDirectory = NSHomeDirectory();//获取用户的根目录
        NSArray *array = @[@123,@456,@"789",@"88888"];
        
        NSString *drectory = [homeDirectory stringByAppendingPathComponent:@"array.archive"];
        BOOL isSuccess = [NSKeyedArchiver archiveRootObject:array toFile:drectory];
        if(isSuccess){
            NSLog(@"成功序列化........");
        }

Objective-c中对象的归档_第1张图片

解档

//解归档,反序列化
    NSArray *array1 = [NSKeyedUnarchiver unarchiveObjectWithFile:drectory];
    
    NSLog(@"%@",array1);

Objective-c中对象的归档_第2张图片

使用这种方法的归档的缺点是每个对象都要归档一个文件,对象多归档的文件也多,


自定义内容归档( 该种方法的归档使用的更方便

   归档

       使用NSData实例作为作为归档的存储数据

       添加归档的内容(设置key与value)

       完成归档

       将归档数据存入磁盘中

   解归档

       从磁盘读取文件,生成NSData实例

       根据Data实例创建和初始化解归档实例

       解归档,根据key访问value的值

//归档
        NSString *homeDirectory = NSHomeDirectory();
        NSString *filePath = [homeDirectory stringByAppendingPathComponent:@"customMsg.archive"];
        NSMutableData *data = [NSMutableData data];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
        
        [archiver encodeInt:1000 forKey:@"num"];
        [archiver encodeObject:@"jimGreen" forKey:@"name"];
        [archiver finishEncoding];
        [archiver release];
        [data writeToFile:filePath atomically:YES];
Objective-c中对象的归档_第3张图片
//解归档
        NSData *myData = [NSData dataWithContentsOfFile:filePath];
        NSKeyedArchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:myData];
        int num = [unarchiver decodeIntForKey:@"num"];
        NSString *name = [unarchiver decodeObjectForKey:@"name"];
        
        NSLog(@"name id %@,age is %d",name,num);
 
 


2、自定义对象归档

自定义的对象要支持归档,需要实现NSCoding协议,NSCoding协议有两个方法encodeWithCoder方法对对象的属性数据进行编码处理,initWithCoder解码归档数据来初始化对象。

 实现NSCoding协议后就能用NSKeyedArchiver就行归档。

下面看个例子:

对User类进行解归档

.h文件

#import <Foundation/Foundation.h>

@interface User : NSObject<NSCoding>

@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *email;
@property(nonatomic,copy)NSString *password;
@property(nonatomic,assign)int age;

@end

.m文件

#import "User.h"

@implementation User

//对属性进行编码,归档的时候会调用
-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeInt:_age forKey:@"age"];
    [aCoder encodeObject:_email forKey:@"email"];
    [aCoder encodeObject:_name forKey:@"name"];
    [aCoder encodeObject:_password forKey:@"password"];
}

//对属性进行解码,解档的时候会用
-(id)initWithCoder:(NSCoder *)aDecoder{
 
    if(self = [super init]){
        
        _age = [aDecoder decodeIntForKey:@"age"];
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.email = [aDecoder decodeObjectForKey:@"email"];
        self.password = [aDecoder decodeObjectForKey:@"password"];
    }
    return self;
}

//该方法是当在以%@格式打印对象的时候调用
-(NSString *)description{
    
    NSString *str = [NSString stringWithFormat:@"name is %@,age id %d,email is %@,password id %@",_name,_age,_email,_password];
    
    return str;
}

-(void)dealloc{
    [_password release];
    [_email release];
    [_name release];
    [super dealloc];
}

@end


main文件中的归档

    User *user = [[User alloc]init];
        user.age = 24;
        user.name = @"jim Green";
        user.email = @"[email protected]";
        user.password = @"123456";
        
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"user.archive"];
        BOOL isSuccess = [NSKeyedArchiver archiveRootObject:user toFile:path];
        if (isSuccess) {
            NSLog(@"成功!");
            
        [user release];
        
        //自定义对象的解归档
            
            
        }

Objective-c中对象的归档_第4张图片

解档

User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            NSLog(@"%@",user);
            

Objective-c中对象的归档_第5张图片



你可能感兴趣的:(Objective-C,Objective-C,Objective-C,Objective-C,归档)