Archiver是持久化数据的一种方式,他跟 Plist的差别在于他能持久化自己定义对象。但他没Plist那么方便。

Archiver默认能持久化的数据有NSNumber,NSArray,NSDictionary,NSString,NSData,由于这几个对象已经实现了

<NSCoding>协议。如果我们要实现一个对象的Archiver持久化 ,也必须实现该对象。

1.<NSCoding>协议主要为归档/恢复文件两个方法

//恢复归档文件为对象
-(id)initWithCoder:(NSCoder *)aDecoder
//归档,使对象持久化
-(void)encodeWithCoder:(NSCoder *)aCoder

----------------

例如以下 。我们首先获取归档文件的路径

#pragma mark 获取文件路径
- (NSString *) filePath
{
NSArray *dirPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
NSString *dirPath=dirPaths[0];
NSString *filePath=[dirPath stringByAppendingPathComponent:@"aa.archiver"];
return filePath;
}

2.系统默认对象怎样归档(NSNumber,NSArray,NSDictionary,NSString,NSData)

#pragma mark 归档/恢复 Array对象
- (void) savearray
{ NSString *filePath=[self filePath];
//
// NSArray *arr=@[@"ttt",@"BBB",@25];
// [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
//
NSArray *arr1=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr1);
}
#pragma mark 归档/恢复 Dictionary对象
- (void) saveDic
{
NSString *filePath=[self filePath];
// NSDictionary *dict=@{@"name":@"lean",@"age":@25};
// BOOL flag=[NSKeyedArchiver archiveRootObject:dict toFile:filePath];
// NSLog(@"%d",flag);
NSDictionary *dict2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",dict2);
}

3.怎样归档自己定义对象。

定义了一个Person类。例如以下:

#import <Foundation/Foundation.h>

@interface Person : NSObject <NSCoding>

@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age; + (Person *) initWithName:(NSString *)name andAge:(int) age; @end #import "Person.h" @implementation Person + (Person *) initWithName:(NSString *)name andAge:(int) age
{
Person *p=[[Person alloc] init];
p.name=name;
p.age=age;
return p;
} -(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
} -(id)initWithCoder:(NSCoder *)aDecoder
{
[self setName:[aDecoder decodeObjectForKey:@"name"]];
[self setAge:[aDecoder decodeIntForKey:@"age"]];
return self;
} @end

TIP: 无论是encode还是decode 都是依据对象的类型去选用不同的方法。如

encodeInt:forkey:      encodeDouble:forkey:   encodeFloat:forkey:

decodeObjectForKey:  decodeIntForKey:  decodeDoubleForKey:

NSKeyedArchiver archiveRootObject:toFile:

NSKeyedUnarchiver unarchiveObjectWithFile:

各自是对须要归档。

恢复的对象进行操作的两个类

定义完了Person类后,在须要归档的地方调用例如以下:

#pragma mark 归档/恢复 自己定义对象
- (void) savePerson
{
NSString *filePath=[self filePath];
Person *p=[Person initWithName:@"lean" andAge:22];
BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
Person *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d-%d",flag,p2.age);
}

对于其Person类,如果该类中还有自己定义对象作为属性。相同实现<NSCoding>协议

4.如果该对象是某个对象子类,这里我们建立一个叫Student类作为Person的子类

#import "Person.h"

@interface Student : Person

@property (nonatomic ,assign) int no;

+ (Student *) initWithName:(NSString *)name andAge:(int) age andNO:(int) no;

@end

相同Student也须要实现NSCoding协议的方法

-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super initWithCoder:aDecoder]) {
[self setNo:[aDecoder decodeIntForKey:@"no"]];
}
return self;
} -(void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeInt:self.no forKey:@"no"];
}
#pragma mark 归档/恢复 自己定义子类对象
- (void) saveStudent
{
NSString *filePath=[self filePath];
Student *p=[Student initWithName:@"lean" andAge:22 andNO:150133];
BOOL flag=[NSKeyedArchiver archiveRootObject:p toFile:filePath];
Student *p2=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%d-%@",flag,p2.name);
}

IOS-Archiver文件归档(2)的更多相关文章

  1. iOS:文件归档和解归档的详解和使用

    文件归档和解归档: 用途: 所谓文件归档,就是把需要存储的对象数据存储到沙盒的Documents目录下的文件中,即存储到了磁盘上,实现数据的持久性存储和备份.解归档,就是从磁盘上读取该文件下的数据,用 ...

  2. IOS三种归档(NSKeyArchieve)的总结

    IOS三种归档(NSKeyArchieve)的总结 归档是一种IOS中常用来存储文件的一种方法,在面向对象的语言中,归档也就实际上可以将一切对象存储在文件中,以下是IOS开发中常见的三种文件归档方式, ...

  3. iOS应用文件夹

    IOS5多了一个比较重要的功能iCloud,但是同时也出现一个问题,很多的APP都把很大量的数据存在APP底下的Documents(/Documents )文件夹里面,这样苹果会reject掉你的AP ...

  4. linux专题一之文件归档和压缩(tar、file、zip)

     本文主要从以下几个方便来说明文件的归档和压缩,同时比较几种不同压缩方法的压缩比率及特点. 文件归档命令tar,tar.gz源码包的安装管理 创建tar包-解压-查询tar包内容 zip命令的用法 为 ...

  5. iOS: Crash文件解析(一)

    iOS Crash文件的解析(一) 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断. ...

  6. iOS 获取文件的目录路径的几种方法 [转]

    iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...

  7. RHEL7文件归档与压缩

    本文介绍RHEL7.2文件的归档和压缩 文件归档 归档的好处:方便使用.查询.阅读,易于管理 (批量删除文件) 常用操作 命令:tar 作用:将许多文件一起保存至一个单独的磁带或磁盘归档,并能从归档中 ...

  8. iOS Crash文件的解析

    iOS Crash文件的解析 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断.联想起 ...

  9. iOS: 获取文件路径

    iOS: 获取文件路径   // 例如 - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectories ...

  10. iOS实现文件上传功能模块

    iOS实现文件上传功能,首先要知道的是,上传到服务器的数据格式,一般采用HTTP文件上传协议.如下图 如图所示,只要设置好了HTTP的协议格式,就可以实现文件上传功能. 代码如下: //图片上传模块 ...

随机推荐

  1. zoj 3757&&3758

    3757一个模拟题,简单,但容易错: 3758 大素数判定就行: #include<cstdio> #include<cstring> #include<algorith ...

  2. MongoDB开发应用实战

    http://special.csdn.net/mongodb/ http://www.csdn.net/article/2011-03-21/294271 http://blog.itpub.net ...

  3. HDU 2986 Ballot evaluation(精度问题)

    点我看题目 题意 : 给你n个人名,每个名后边跟着一个数,然后m个式子,判断是否正确. 思路 :算是一个模拟吧,但是要注意浮点数容易丢失精度,所以要好好处理精度,不知道多少人死在精度上,不过我实在是不 ...

  4. php smarty 配置四个存放目录

    require("Smarty.class.php"); $smarty = new Smarty(); $smarty -> template_dir = "./ ...

  5. Java集合类之向量Vector

    package com.test; import java.util.*; public class Demo7_3 { public static void main(String[] args) ...

  6. FireMonkey vs. VCL (FMX的UI更灵活,图形效果更强,硬件加速,内嵌3D,使用浮点数更精确,跨平台,可使用Mida converter转换和TFireMonkeyContainer内嵌)

    Frequently when I am talking about the VCL or FireMonkey I get some of these common questions: Is VC ...

  7. Java:List,ArrayList和LinkList的区别

    1.大学数据结构中ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构 2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList ...

  8. apache开源项目--ibatis

    iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目.最初侧重于密码软件的开发,现在是一个基于Java的持久层框架.i ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.3

    Use the QR decomposition to prove Hadamard's inequality: if $X=(x_1,\cdots,x_n)$, then $$\bex |\det ...

  10. Linux下的nginx启动、重新启动

    nginx的启动命令是: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -c制定配置文件的路径,不加-nginx会自动 ...