示例:多个不同对象存储到同一个归档文件中
#import <Foundation/Foundation.h>
@interface Foo : NSObject <NSCoding>
@property (copy, nonatomic) NSString *strVal;
@property int intVal;
@property float floatVal;
-(void) encodeWithCoder: (NSCoder *) encoder;
-(id) initWithCoder: (NSCoder *) decoder;
@end
@implementation Foo
@synthesize strVal, intVal, floatVal;
-(void) encodeWithCoder: (NSCoder *) encoder
{
//[super encodeWithCoder: encoder];//如果有父类
// key通常由类名+变量名组成
[encoder encodeObject: strVal forKey: @"FoostrVal"];
[encoder encodeInt: intVal forKey: @"FoointVal"];
[encoder encodeFloat: floatVal forKey: @"FoofloatVal"];
}
-(id) initWithCoder: (NSCoder *) decoder
{
strVal = [decoder decodeObjectForKey: @"FoostrVal"];
intVal = [decoder decodeIntForKey: @"FoointVal"];
floatVal = [decoder decodeFloatForKey: @"FoofloatVal"];
return self;
}
@end
//-------------------------Moo类----------------------------------
@interface Moo : NSObject <NSCoding>
@property (copy, nonatomic) NSString *strVal;
@property int intVal;
@property float floatVal;
-(void) encodeWithCoder: (NSCoder *) encoder;
-(id) initWithCoder: (NSCoder *) decoder;
@end
@implementation Moo
@synthesize strVal, intVal, floatVal;
-(void) encodeWithCoder: (NSCoder *) encoder
{
//[super encodeWithCoder: encoder];//如果有父类
// key通常由类名+变量名组成
[encoder encodeObject: strVal forKey: @"FoostrVal"];
[encoder encodeInt: intVal forKey: @"FoointVal"];
[encoder encodeFloat: floatVal forKey: @"FoofloatVal"];
}
-(id) initWithCoder: (NSCoder *) decoder
{
strVal = [decoder decodeObjectForKey: @"FoostrVal"];
intVal = [decoder decodeIntForKey: @"FoointVal"];
floatVal = [decoder decodeFloatForKey: @"FoofloatVal"];
return self;
}
@end
//------------------------- Main -----------------------
int main(int argc, const char * argv[]) {
@autoreleasepool {
Foo *myFoo = [[Foo alloc] init];
Moo *myMoo = [[Moo alloc] init];
NSMutableData *dataArea;
NSKeyedArchiver *archiver;
myFoo.strVal = @"[Foo] This is the string";
myFoo.intVal = 100;
myFoo.floatVal = 1.1;
myMoo = [[Moo alloc] init];
myMoo.strVal = @"[Moo] this is the string";
myMoo.intVal = 200;
myMoo.floatVal = 2.2;
// 设置数据区,并将其连接到一个NSKeyedArchiver对象
dataArea = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: dataArea];
// 开始存档
[archiver encodeObject: myFoo forKey: @"myfoo"];
[archiver encodeObject: myMoo forKey: @"mymoo"];
[archiver finishEncoding];
// 将存档的数据区写到文件
if ([dataArea writeToFile: @"myArchive" atomically: YES] == NO)
NSLog(@"Archiving failed!");
//------------- 读取归档数据 ---------------
NSKeyedUnarchiver *unarchiver;
NSData *dataArea1;
Foo *myFoo1;
Moo *myMoo1;
// 从归档文件中读取并连接NSKeyedUnarchiver对象
dataArea1 = [NSData dataWithContentsOfFile: @"myArchive"];
if (!dataArea1) {
NSLog(@"Can't read back archive file!");
return 1;
}
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dataArea1];
// 解码以前存储在归档文件中的对象
myFoo1 = [unarchiver decodeObjectForKey: @"myfoo"];
myMoo1 = [unarchiver decodeObjectForKey: @"mymoo"];
[unarchiver finishDecoding];
// 验证是否还原成功
NSLog(@"\n [myFoo1] strVal=%@, intVal=%i, floatVal=%g",
myFoo1.strVal, myFoo1.intVal, myFoo1.floatVal);
NSLog(@"\n [myMoo1] strVal=%@, intVal=%i, floatVal=%g",
myMoo1.strVal, myMoo1.intVal, myMoo1.floatVal);
}
return 0;
}
运行测试