NSKeyedArchiver NSKeyedUnarchiver NSData MRC编程

1.归档解档基本对象

如果对象是NSStringNSDictionaryNSArrayNSDataNSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复
不是所有的对象都可以直接用这种方法进行归档,只有遵守了NSCoding协议的对象才可以

#define path [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"array.archive"]

- (void)archiveArray{
    // 归档一个NSArray对象到Documents/array.archive
    NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
    [NSKeyedArchiver archiveRootObject:array toFile:path];
}

- (void)unarchiveArray{
    // 恢复(解码)NSArray对象
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@",array);
}

// /Users/admin/Library/Developer/CoreSimulator/Devices/
95A0E48B-2AF9-45A0-83AE-6C065C293B5E/data/Containers/Data/Application/
3B971E73-4F85-4FDC-B882-55640879AB9F/Documents/array.archive

(
    a,
    b,
    c
)
NSKeyedArchiver NSKeyedUnarchiver <NSCoding> NSData MRC编程_第1张图片
1-归档为二进制文件.png

2.MRC环境下的归档解档

NSKeyedArchiver NSKeyedUnarchiver <NSCoding> NSData MRC编程_第2张图片
2-MRC环境.png

Person.h

#import 
@interface Person : NSObject 
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int age;
@property (nonatomic, assign) float height;
@end

Person.m

#import "Person.h"

@implementation Person
/**
 *  编码归档
 *  每次归档对象时,都会调用这个方法。
 *  一般在这个方法里面指定如何归档对象中的每个实例变量,
 *  可以使用encodeObject:forKey:方法归档实例变量
 */
- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.name forKey:@"name"];
    [encoder encodeInt:self.age forKey:@"age"];
    [encoder encodeFloat:self.height forKey:@"height"];
}

/**
 *  每次从文件中恢复(解码)对象时,都会调用这个方法。
 *  一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,
 *  可以使用decodeObject:forKey:方法解码实例变量
 */
- (id)initWithCoder:(NSCoder *)decoder
{
    self.name = [decoder decodeObjectForKey:@"name"];
    self.age = [decoder decodeIntForKey:@"age"];
    self.height = [decoder decodeFloatForKey:@"height"];
    return self;
}

// -fno-objc-arc
- (void)dealloc{
    [super dealloc];
    
    [_name release]; // OC对象在销毁时需要release 
}
@end

如果父类也遵守了NSCoding协议,请注意:
应该在encodeWithCoder:方法中加上一句

[super encodeWithCoder:encoder];

确保继承的实例变量也能被编码,即也能被归档
应该在initWithCoder:方法中加上一句

self = [super initWithCoder:decoder];

确保继承的实例变量也能被解码,即也能被恢复

- (void)archivePerson{
    Person *person  = [[[Person alloc] init] autorelease];
    person.name = @"solozyx";
    person.age = 99;
    person.height = 1.72f;
    [NSKeyedArchiver archiveRootObject:person toFile:path];
}

- (void)unarchivePerson{
    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@",person.name);
    NSLog(@"%d",person.age);
    NSLog(@"%f",person.height);
}

//2016-08-23 14:40:05.537 偏好设置[91371:1353084] solozyx
//2016-08-23 14:40:05.537 偏好设置[91371:1353084] 99
//2016-08-23 14:40:05.537 偏好设置[91371:1353084] 1.720000

3.NSMutableData 归档和解档多个对象

使用archiveRootObject:toFile:方法可以将一个对象直接写入到一个文件中,但有时候可能想将多个对象写入到同一个文件中,那么就要使用NSData来进行归档对象.NSData可以为一些数据提供临时存储空间,以便随后写入文件,或者存放从磁盘读取的文件内容。
可以使用[NSMutableData data]创建可变数据空间

#define path [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"2person.archive"]

- (void)archive2Persons{
    // NSData : 归档2个Person对象到同一文件中
    // 新建一块可变数据区
    NSMutableData *data = [NSMutableData data];
    // 将数据区连接到一个NSKeyedArchiver对象
    NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
    
    Person *person1 = [[[Person alloc] init] autorelease];
    person1.name = @"person1";
    person1.age = 1;
    person1.height = 1.0f;
    
    Person *person2 = [[[Person alloc] init] autorelease];
    person2.name = @"person2";
    person2.age = 2;
    person2.height = 2.0f;
    
    // 开始存档对象,存档的数据都会存储到NSMutableData中
    [archiver encodeObject:person1 forKey:@"person1"];
    [archiver encodeObject:person2 forKey:@"person2"];
    
    // 存档完毕(一定要调用这个方法)
    [archiver finishEncoding];
    
    // 将存档的数据写入文件
    [data writeToFile:path atomically:YES];
}

- (void)unarchive2Persons{
    // NSData : 从同一文件中恢复2个Person对象
    // 从文件中读取数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    // 根据数据,解析成一个NSKeyedUnarchiver对象
    NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
    
    Person *person1 = [unarchiver decodeObjectForKey:@"person1"];
    Person *person2 = [unarchiver decodeObjectForKey:@"person2"];
    
    // 恢复完毕
    [unarchiver finishDecoding];
    
    NSLog(@"%@ - %d - %f",person1.name,(int)person1.age,(float)person1.height);
    NSLog(@"%@ - %d - %f",person2.name,(int)person2.age,(float)person2.height);
}

//2016-08-23 14:54:25.544 偏好设置[92025:1363219] person1 - 1 - 1.000000
//2016-08-23 14:54:25.545 偏好设置[92025:1363219] person2 - 2 - 2.000000
NSKeyedArchiver NSKeyedUnarchiver <NSCoding> NSData MRC编程_第3张图片
3-归档多个对象.png

4.深复制浅复制

Student.h

#import "Person.h"
@interface Student : Person
@end

Student.m

#import "Student.h"
@implementation Student
@end
Person *person1 = [[[Person alloc] init] autorelease];
person1.name = @"person1";
person1.age = 1;
person1.height = 1.0f;

// 比如对一个Person对象进行深复制

// 临时存储person1的数据
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:person1];
// 解析data,生成一个新的Person对象
Student *person2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];

// 分别打印内存地址
NSLog(@"person1:%p", person1);
NSLog(@"person2:%p", person2);

//2016-08-23 15:05:56.944 偏好设置[92737:1375891] person1:0x7fc781c1fef0
//2016-08-23 15:05:56.944 偏好设置[92737:1375891] person2:0x7fc781f077b0
NSKeyedArchiver NSKeyedUnarchiver <NSCoding> NSData MRC编程_第4张图片
4-归档解档.png
NSKeyedArchiver NSKeyedUnarchiver <NSCoding> NSData MRC编程_第5张图片
5-NSData的作用.png
6-利用归档实现深复制.png

你可能感兴趣的:(NSKeyedArchiver NSKeyedUnarchiver NSData MRC编程)