#import <Foundation/Foundation.h>
@interface TestCodingProtocol : NSObject<NSCoding>
#pragma mark- 持久化属性
@property(nonatomic,copy,readwrite) NSString* myName;
@property(nonatomic,assign,readwrite)NSInteger myIndex;
#pragma mark- 实现协议方法
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end
@interface TestCodingProtocolChild : TestCodingProtocol
@property(nonatomic,assign,readwrite) NSInteger myIndexChild;
-(void)encodeWithCoder:(NSCoder *)aCoder;
-(id)initWithCoder:(NSCoder *)aDecoder;
@end
#import "TestCodingProtocol.h"
@implementation TestCodingProtocol
-(void)encodeWithCoder:(NSCoder *)aCoder
{
if (aCoder) {
[aCoder encodeObject:self.myName forKey:@"myName"];
//self.myIndex为基础类型,不能用encodeObject
[aCoder encodeInteger:self.myIndex forKey:@"myIndex"];
}
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
if (!aDecoder) {
return self;
}
self.myName = [aDecoder decodeObjectForKey:@"myName"];
self.myIndex = [aDecoder decodeIntegerForKey:@"myIndex"];
}
return self;
}
@end
@implementation TestCodingProtocolChild
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeInteger:self.myIndexChild forKey:@"myIndexChild"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {//这里如果用init的话,父类的内容得不到初始化。
if (!aDecoder) {
return self;
}
self.myIndexChild = [aDecoder decodeIntegerForKey:@"myIndexChild"];
}
return self;
}
@end
NSString* path = @"/Users/liyanq/Desktop";
NSString* fPath = [path stringByAppendingPathComponent:@"copying.plist"];
TestCodingProtocol* t = [[TestCodingProtocol alloc] init];
t.myIndex = 5;
t.myName = @"hello";
TestCodingProtocolChild* tChild = [[TestCodingProtocolChild alloc] init];
tChild.myIndex = 6;
tChild.myName = @"child";
tChild.myIndexChild = 7;
//增加几个基础类型。
NSString* s = @"helloworld";
NSNumber* n = [NSNumber numberWithInteger:5];
NSDate* d = [NSDate date];
NSArray* r = [NSArray arrayWithObjects:tChild,t,s,n,d, nil];
[NSKeyedArchiver archiveRootObject:r toFile:fPath];<span style="color: rgb(76, 191, 87); font-size: 11px; font-family: Menlo;">//</span><span style="color: rgb(76, 191, 87); font-family: 'Heiti SC Light'; font-size: 11px;">完成了存储。没有文件会创建,有的话会清除内容。</span>
NSArray* er = [NSKeyedUnarchiver unarchiveObjectWithFile:fPath];//完成了读取。
[er enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[TestCodingProtocolChild class]]) {
TestCodingProtocolChild* buf = obj;//只能声明个变量了,不会强制转换。
NSLog(@"myIndex is %ld, myName is %@, myIndexChild is %ld", buf.myIndex, buf.myName, buf.myIndexChild);
}
else if ([obj isKindOfClass:[TestCodingProtocol class]])
{
TestCodingProtocol* buf = obj;
NSLog(@"myIndex is %ld, myName is %@", buf.myIndex, buf.myName);
}
else{
NSLog(@"index is %ld,value is %@",idx,obj);
}
}];
总结下吧:
1,完成了对象的序列化与反序列化,而且对象可以继承自nsobject.
2,归档的形式来保存数据,只能一次性归档保存以及一次性解压。所以只能针对小量数据,而且对数据操作比较笨拙,即如果想改动数据的某一小部分,还是需要解压整个数据或者归档整个数据。
3,使用NSKeyedArchiver可以归档和恢复NSString、NSArray、NSDictionary、NSSet、NSDate、NSNumber和NSData等基本的Foundation对象
参考:http://blog.csdn.net/tianyitianyi1/article/details/7713103