IOS-Archiver文件归档(2)

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 *[email protected][@"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 *[email protected]{@"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);
}
时间: 2024-12-19 11:42:54

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

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

文件归档和解归档: 用途: 所谓文件归档,就是把需要存储的对象数据存储到沙盒的Documents目录下的文件中,即存储到了磁盘上,实现数据的持久性存储和备份.解归档,就是从磁盘上读取该文件下的数据,用来完成用户的需求.对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径的读取文件的内容(也称为接档,反序列化),(对象归档的文件是保密的,在磁盘上无法查看文件中的内容,而属性列表是明文的,可以查看). 区别: 通过文件归档产生的文件是不可见的,如果打开

OC-Day8____文件归档

2015.3.9 #import <Foundation/Foundation.h> //plist //代码方式 // //plist 文件的根节点只能是数组或者字典 //plist 文件只能存储 NSString NSArray NSDictionary //NSData NSDate NSNumber BOOL int main(int argc, const char * argv[]) { @autoreleasepool { //        NSFileManager *fm

NSCoding的使用方法---iOS上的归档(增加更改内容的方法).

上一章我们介绍了在iOS上的归档以及解档, 今天我们在归档之后稍微做一些改变, 使得解档之后得到的结果有所不同, 这个方法类似NSMutableXXX, 可修改里面的参数, 下面让我们来看看吧. 涉及的方法: seekToFileOffset:这个方法是寻求方法的偏移, 意思就是在文件中寻找文本里的起点. readDataOfLength:这个方法是指读取文件的长度是多少. offsetInFile:是指写到第几个位置. #import "ViewController.h" #defi

NSCoding的使用方法---iOS上的归档.

在iOS上的归档和在Mac上的归档有些不一样, 在Mac上的归档你可以把文件放在任意的一个文件夹里面, 但是在iOS上, 你所写的文件就只能放在三个文件夹里, 分别是Documents, Library, tmp三个文件, 这里需要注意一下, library和tmp文件会在软件升级, 系统升级或者系统空间不足时会自动清除里面的文件, 只有在Documents文件才可以永久保存, 直到你把软件删除为止. 这里涉及的方法: NSHomeDirectory:这个方法的意思就是获取软件的主目录. str

《Linux菜鸟入门》不同系统文件传输及文件归档压缩

ip addr show br0 查看真实主机的ip ● 文件归档 1. 文件归档是把多个文件变成一个归档文件 参数 tar  c             创建 f             指定归档名称 t             显示归档文件的内容 r             向归档文件内继续添加文件 --get              从归档文件中取出单个文件 --delete                删除单个文件 x             取出归档文件中的所有文件 -C     

iOS: Crash文件解析(一)

iOS Crash文件的解析(一) 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断.联想起老罗在发布Smartisan OS的时候说了,他准备了10个手机,如果一台有问题,就换一台,如果10台后挂了他就不做手机了.好了不闲扯了,今天就跟大家一起聊聊iOSCrash文件的组成以及常用的分析工具. 有一个WWDC 2010的视频推荐大家抽空看看,视频名称“Understanding C

iOS 创建文件夹,删除文件夹,对文件夹重命名的操作

iOS 创建文件夹,删除文件夹,对文件夹重命名的操作 by 伍雪颖 + (void)createFolder:(NSString *)folderName { NSString *imageDir = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(),folderName]; NSLog(@"HomeDir: %@",imageDir); BOOL isDir = NO; NSFileM

IOS之文件的写入和读出

// 获取文件路径 /** 1 * bundle是一个目录,其中包含应用程序的所有资源,通过mainBundle 得到这个目录后就可以获取resource下的资源 */ NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ContactsInfo" ofType:nil]; NSLog(@"%@", filePath); // 将文件中的内容取出来 存储成字符串 有了其中的内容就可以做一些相应的

文件归档、压缩及传输

1.文件归档文件归档,就是把多个文件变成一个归档文件 tar    c                 创建         f                 指定归档文件名称         t                 显示归档文件中的内容         r                 向归档文件中添加文件    --get               取出单个文件    --delete         删除单个文件        x                 取出归档

ios 得到文件夹大小 进率是1000

- (CGFloat)folderSizeAtPath:(NSString *) folderPath { NSFileManager * manager = [NSFileManager defaultManager]; if (![manager fileExistsAtPath:folderPath]) { return 0; } NSEnumerator * childFilesEnumerator = [[manager subpathsAtPath:folderPath] objec