文章目录
  1. 1. 使用NSKeyedArchiver归档
  2. 2. 实现NSCoding协议
  3. 3. 使用NSData完成自定义归档
  • 归档就是用户某种格式把一个或者多个对象保存到指定的文件中,方便以后从文件中恢复它们。

使用NSKeyedArchiver归档

  • NSKeyedArchiver负责将对象归档到指定文件中
  • NSKeyedUnarchiver则负责从文件中恢复对象

    • 直接调用archivedDataWithRootObject:dataarchiveRootObject:toFile:file类方法将指定对象作为root进行归档;恢复时,则调用NSKeyedUnarchiver的unarchiveObjectWithData:data或者unarchiveObjectWithFile:file类方法。

      1
      2
      3
      4
      5
      6
      7
      // 归档
      NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithInt:89],@"objc",
      [NSNumber numberWithInt:88],@"cpp",
      [NSNumber numberWithInt:87],@"java",
      nil];
      [NSKeyedArchiver archiveRootObject:dict toFile:@"myDict.archive"];
      1
      2
      3
      4
      5
      // 恢复
      NSDictionary* dict = [NSKeyedUnarchiver unarchiveObjectWithFile:@"myDict.archive"];
      NSLog("%d",[dict valueForKey:@"objc"]);
      NSLog("%d",[dict valueForKey:@"cpp"]);
      NSLog("%d",[dict valueForKey:@"java"]);

实现NSCoding协议

  • 自定义类要实现归档、恢复,应该实现NSCoding协议,必须实现该协议中的定义的如下两个方法
    • initWithCoder:该方法负责恢复对象
    • encodeWithCoder:该方法负责归档对象
  • 归档和恢复基本数据类型
归档数据 恢复数据
encodeBool:forKey decodeBoolForKey
encodeInt:forKey decodeIntForKey
encodeInt32:forKey decodeInt32ForKey
encodeInt64:forKey decodeInt64ForKey
encodeFloat:forKey decodeFloatForKey
encodeDouble:forKey decodeDoubleForKey
  • 示例
1
2
3
4
5
6
7
# import <Foundation/Foundation.h>
@interface ZTApple : NSObject <NSCoding>
@property (nonatomic,copy) NSString* color;
@property (nonatomic,assign)double weight;
@property (nonatomic,assign)int size;
- (id) initWithColor:(NSString*) color weight:(double) weight size:(int) size;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# import "ZTApple.h"
@implementation ZTApple
@synthesize color = _color;
@synthesize weight = _weight;
@synthesize size = _size;
-(id) initWithColor:(NSString*) color weight:(double) weight size:(int) size
{
if(self = [super init])
{
self.color = color;
self.weight = weight;
self.size = size;
}
return self;
}
-(NSString *)description
{
return [NSString stringWithFormat:"@<ZTApple[_color=%@,_weigt=%g,_size=%d]>",self.color,self.weight,self.size];
}
-(void) encodeWithCoder:(NSCode*) coder
{
[coder encodeOject:_color forKey:@"color"];
[coder encodeDouble:_weight foeKey:@"weight"];
[coder encodeInt:_size forKey:@"size"];
}
-(void) initWithCoder:(NSCoder*) coder
{
_color = [coder decodeObjectForKey:@"color"];
_weight = [coder decodeDoubleForkey:@"weight"];
_size = [coder decodeIntForKey:@"size"];
return self;
}
@end

使用NSData完成自定义归档

  • 一次性收集多个对象,并将这些对象归档到单个文件中,此时借助NSMutableData来创建NSKeyedArchiver或NSKeyedUnarchiver对象。
  • 归档过程
  1. 以NSMutableData作为参数,创建NSKeyedArchiver对象。
  2. 反复调用NSKeyedArchiver对象的encodeXxx:forKey:方法来归档所有需要归档到一个文件的对象。
  3. 调用NSKeyedArchiver对象的finishEncoding方法来结束归档。
  4. 根据需要,程序可以选择将保存归档数据的NSMutableData通过网络或输出到磁盘文件上。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:66],@"objc",
    [NSNumber numberWithInt:76],@"c++",
    [NSNumber numberWithInt:86],@"java",nil];
    NSSet* set = [NSSet setWithObjects:
    @"test1",@"test2",@"test3",nil];
    ZTApple* apple = [[ZTApple alloc] initWithColor:@"红色" weight:3.4 size:20];
    NSMutableData* data = [[NSMutableData data];
    NSKeyedArchiver* arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [arch encodeObject:dict forKey:@"dict"];
    [arch encodeObject:set forKey:@"set"];
    [arch encodeObject:apple forKey:@"apple"];
    [arch finishEncoding];
    if([data writeToFile:@"multi.archive" atomically:YES] == NO)
    {
    NSLog("归档失败");
    }
  • 恢复过程
  1. 以NSData作为参数,创建NSkeyedUnarchiver对象
  2. 重复调用NSKeyedUnarchiver对象的decodeXxx:forKey:方法从文件中恢复所有归档过的对象
  3. 调用NSKeyedUnarchiver对象的finishDecoding方法结束恢复
    1
    2
    3
    4
    5
    6
    NSData* data = [NSData dataWithContentsOfFile:@"multi.archive"];
    NSKeyedUnarchiver* unarch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSDictionary* dict = [unarch decodeObjectForKey:@"dict"];
    NSSet* set = [unarch decodeObjectForKey:@"set"];
    NSApple* apple = [unarch decodeObjectForKey:@"apple"];
    [unarch finishDecoding];
文章目录
  1. 1. 使用NSKeyedArchiver归档
  2. 2. 实现NSCoding协议
  3. 3. 使用NSData完成自定义归档