在面向对象的编程中,会涉及到对象的保存,对象的保存在JAVA当中称之为serialize,也叫序列化。而在cocoa当中,这个过程称之为归档。cocoa当中最最典型的序列化例子就是NIB文件的保存和读取过程。
在cocoa当中如何实现了这个功能的哪?
其实在cocoa当中是通过了NSCoding协议和NSCoder抽象类共同实现的,对于NSCoder抽象类而言,其实他只是一个抽象类,是不能实例化使用的,而真正使用的是NSCoder的子类,即NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver类的实例对象。
那么实现的关键点有两点:
1、要使的需要存储的对象实现NSCoding协议,从而使他自己满足写二进制数据的能力。即是自己具有序列化的能力。
2、使用NSCoder的子类方法,实现二进制数据的动作,如读或者写。即触发存档过程。
需要注意的一点就是说,其实对于序列化,是一个“一传十,十传百”的过程,父类的序列化,会逐层递归的将所有的子对象也进行了序列化。此外,序列化,能够序列化的是对象实例和类名,而对于方法其实是无法序列化的,因此对于序列化和非序列化的类,都需要使用共同的类定义,否则无法还原。
NSCoding协议的方法:
-(id)initWithCoder:(NSCoder *)coder; // 读取coder中的数据
-(void) encodeWithCoder:(NSCoder*)coder;// 向coder中写入数据
NSKeyArchiver的方法
+(NSData*)archivedDataWithRootObject:(id)rootObject
例子:
#imp ort <Cocoa/Cocoa.h>
@interface codeObj : NSObject <NSCoding>{
NSString *name;
int magicNumber;
float shoseSize;
NSMutableArray *subThingies;
}
@property (copy) NSString *name;
@property int magicNumber;
@property float shoseSize;
@property (retain) NSMutableArray *subThingies;
-(id) initwithName:(NSString *) n
magicNumber:(int) mn shoseSize:(float) ss;
@end
// codeObj.m
// encodeObject
//
// Created by 110 on 10-2-6.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#imp ort "codeObj.h"
@implementation codeObj
@synthesize name;
@synthesize magicNumber;
@synthesize shoseSize;
@synthesize subThingies;
-(id) initwithName:(NSString *) n
magicNumber:(int) mn shoseSize:(float) ss{
if(self=[super init]){
self.name=n;
self.magicNumber=mn;
self.shoseSize=ss;
self.subThingies=[NSMutableArray array];
}
return (self);
}
-(void) dealloc{
[name release];
[subThingies release];
[super dealloc];
}
//nscoding协议的方法
-(void) encodeWithCoder:(NSCoder *) coder{
[coder encodeObject:name forKey:@"name"];
[coder encodeInt:magicNumber forKey:@"magicNumber"];
[coder encodeFloat:shoseSize forKey:@"shoseSize"];
[coder encodeObject:subThingies forKey:@"subThingies"];
}
-(id) initWithCoder:(NSCoder *) decoder{
if(self =[super init]){
self.name=[decoder decodeObjectForKey:@"name"];
self.magicNumber=[decoder decodeIntForKey:@"magicNumber"];
self.shoseSize=[decoder decodeFloatForKey:@"shoseSize"];
self.subThingies=[decoder decodeObjectForKey:@"subThingies"];
}
return (self);
}
@end
#import "codeObj.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
codeObj *thing;
thing=[[[codeObj alloc] initwithName:@"name" magicNumber:20 shoseSize:30.5] autorelease];
NSLog(@"--------%@",thing);
NSData *freezeDrid;
freezeDrid=[NSKeyedArchiver archivedDataWithRootObje ct:thing];
[freezeDrid writeToFile:@"/tmp/codeobj.txt" atomically:YES];
codeObj *anotherThing;
anotherThing=[[[codeObj alloc] initwithName:@"ssssss" magicNumber:20 shoseSize:4.5] autorelease];
[anotherThing.subThingies addObject:thing];
NSData *other;
other=[NSKeyedArchiver archivedDataWithRootObje ct:anotherThing];
//写入文件
[other writeToFile:@"/tmp/objandobj.txt" atomically:YES];
//从文件中读取
NSData *fileData;
fileData=[NSData dataWithContentsOfFile:@"/tmp/objandobj.txt"];
codeObj *fromFile;
fromFile=[NSKeyedUnarchiver unarchiveObjectWithData:fileData];
NSLog(@"------%@",fromFile);
[pool drain];
return 0;
}