iOS中数据持久化中的对象归档

对象归档

对象归档实例:Encoding

对象归档

“归档”是值另一种形式的序列化,对模型对象进行归档的技术可以轻松将复杂的对象写入文件,然后再从中读取它们,只要在类中实现的每个属性都是基本数据类型(如int或float)或都是符合NSCoding协议的某个类的实例,你就可以对你的对象进行完整归档。

实现NSCoding协议

NSCoding协议声明了两个方法: -(void)encodeWithCoder:(NSCoder *)aCoder,是将对象写入到文件中。 

-(id)initWithCoder:(NSCoder *)aDecoder,是将文件中数据读入到对象中。

实现NSCopying协议

NSCopying协议声明了一个方法: -(id)copyWithZone:(NSZone *)zone ,是将对象复制方法。 

Student.h

@interface Student : NSObject<NSCoding, NSCopying>

@property (retain, nonatomic) NSString* studentNo;
@property (retain, nonatomic) NSString* studentName;
@property (retain, nonatomic) NSString* studentClass;

@end

Student.m

#import "Student.h"

@implementation Student

@synthesize studentNo = _studentNo;
@synthesize studentName = _studentName;
@synthesize studentClass = _studentClass;

#pragma mark NSCopying

- (id)copyWithZone:(NSZone *)zone {
    Student* copy = [[[self class]allocWithZone:zone]init];
    copy.studentNo = [_studentNo copyWithZone:zone];
    copy.studentName = [_studentName copyWithZone:zone];
    copy.studentClass = [_studentClass copyWithZone:zone];
    return copy;
}

#pragma mark  NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_studentNo forKey:@"studentNo"];
    [aCoder encodeObject:_studentName forKey:@"studentName"];
    [aCoder encodeObject:_studentClass forKey:@"studentClass"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
    _studentNo = [aDecoder decodeObjectForKey:@"studentNo"];
    _studentName = [aDecoder decodeObjectForKey:@"studentName"];
    _studentClass = [aDecoder decodeObjectForKey:@"studentClass"];
    return self;
}
-(NSString*)description {
    return [[[NSString alloc]initWithFormat:@"no:%@ name:%@ class:%@", _studentNo, _studentName, _studentClass]autorelease];
}

- (void)dealloc {
    [_studentName release];
    [_studentClass release];
    [_studentNo release];
    [super dealloc];
}
@end

EncodingViewController.m

- (IBAction)saveToArchiver:(id)sender {
    NSString* fileName = [self filePath:@"student.archiver"];
    NSMutableData* data = [NSMutableData data];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    Student* student = [[Student alloc]init];
    student.studentNo = studentNo.text;
    student.studentName = studentName.text;
    student.studentClass = studentClass.text;
    [archiver encodeObject:student forKey:@"myStudent"];
    [archiver finishEncoding];
    [data writeToFile:fileName atomically:YES];
    [archiver release];
    [student release];
}

NSMutableData * theData = [NSMutableData data];用于包含编码的数据。

NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];创建NSKeyedArchiver实例,用于将对象归档到此theData实例中。

[archiver encodeObject:student forKey:@"mystudent"]; 使用“键-值”对编码来对希望包含在归档中的对象进行归档。

[theData writeToFile:filename atomically:YES]; 写入数据到归档文件。

 EncodingViewController.m

- (IBAction)loadFromArchiver:(id)sender {
    NSString* fileName = [self filePath:@"student.archiver"];
    NSData* data = [NSData dataWithContentsOfFile:fileName];
    if ([data length] > 0) {
        NSKeyedUnarchiver* unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
        Student* student = [unArchiver decodeObjectForKey:@"myStudent"];
        studentNo.text = student.studentNo;
        studentName.text = student.studentName;
        studentClass.text = student.studentClass;
        [unArchiver finishDecoding];
        [unArchiver release];
    }
}

NSData * theData =[NSData dataWithContentsOfFile:filename];从归档文件中获得NSData实例。

NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData]; 

创建一个NSKeyedUnarchiver实例对数据进行解码。Student *student = [archiver decodeObjectForKey:@"mystudent"];

使用与归档编码使用相同的键对象进行解码。

原文地址:https://www.cnblogs.com/zhaochaobin/p/5303985.html