iOS---归档与解档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS---归档与解档相关的知识,希望对你有一定的参考价值。

参考技术A /*
ios归档小结:
1.归档实际上就是一种文件保存的形式,几乎所有的对象都能够被归档存储。它使用NSKeyedArchiver进行归档(归档的数据为:二进制数据)、NSKeyedUnarchiver进行解档,它会在写入、读出数据(二进制数据)之前对数据进行序列化、反序列化操作。

/*
单一对象的归档

NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径
NSString* archivePath = [path stringByAppendingPathComponent:@"archive.plist"];
NSLog(@"%@",archivePath);
NSArray* arrMut =@[@@"asd":@"12",@"asd1":@"12",@"asd2":@"12",@@"asd2":@"12"];
//归档--序列化
[NSKeyedArchiver archiveRootObject:arrMut toFile:archivePath];

NSArray* unArr = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];//解档---反序列化
NSLog(@"%@",unArr);

*/

-------------------归档和解档结合Model实现-----------------------------

@interface Worker : NSObject<NSCoding>

@property (nonatomic,strong) NSString* name;//姓名
@property (nonatomic,assign) NSString* age;//年龄
@property (nonatomic,strong) NSString* workType;//职业
@property (nonatomic,assign) NSString* wage;//工资
@property (nonatomic,assign) NSString* workTime;//工作年限

@property (nonatomic,strong) NSDictionary* dict;

@end

@implementation Worker

// self = [super init];
if ( self = [super init])





具体用法:
NSDictionary* dict = @@"name":@"zxc",@"age":@"5",@"workType":@"iOS",@"wage":@"50",@"workTime":@"2";
Worker* work = [[Worker alloc]initWithDict:dict];
NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];//缓存路径

用runtime来重写Coder和deCode方法 归档解档的时候使用

当我们归档自定义对象的时候,可以重写自定义Model的的encodeWithCoder和initWithCoder

开始的大概是这样的,当属性非常多的时候 这种方式就会觉得不还好 好像重复在做一样的事情 其实这块我们可以交给runtime来做

- (void)encodeWithCoder:(NSCoder *)aCoder //将属性进行编码
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
    [aCoder encodeInteger:self.age forKey:@"age"];   
}
- (id)initWithCoder:(NSCoder *)aDecoder //将属性进行解码
{
    NSString *name1 = [aDecoder decodeObjectForKey:@"name"];
    NSString *phone1 = [aDecoder decodeObjectForKey:@"phone"];
    int age1 = [aDecoder decodeIntegerForKey:@"age"];
    [self initWithName:name1 phone:phone1 age:age1];    
    return self;
}

 

用runtime来实现上面的功能

-(void)encodeWithCoder:(NSCoder *)encoder{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([User class], &count);
    for (int i=0; i<count; i++) {
        Ivar iv = ivar[i];
        const char *name = ivar_getName(iv);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [encoder encodeObject:value forKey:strName];
    }
    free(ivar);
}

-(id)initWithCoder:(NSCoder *)decoder{
    self  = [super init];
    if (self) {
        unsigned int count = 0;
        //获取类中所有成员变量名
        Ivar *ivar = class_copyIvarList([User class], &count);
        for (int i = 0; i<count; i++) {
            Ivar iva = ivar[i];
            const char *name = ivar_getName(iva);
            NSString *strName = [NSString stringWithUTF8String:name];
            //进行解档取值
            id value = [decoder decodeObjectForKey:strName];
            //利用KVC对属性赋值
            [self setValue:value forKey:strName];
        }
        free(ivar);
    }
    return self;
}

 

以上是关于iOS---归档与解档的主要内容,如果未能解决你的问题,请参考以下文章

Linux文件归档压缩与解压缩

ios 归档解档

iOS 归档解档

iOS解档与归档

Swift 使用Runtime对模型进行归档解档

iOS 数据储存--归档