//归档        NSDictionary *aDic=[[NSDictionary alloc]initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3", nil];                NSArray *array=[[NSArray alloc]initWithObjects:@"One",@"Two",@"Three", nil];                NSMutableData *data1=[[NSMutableData alloc]init];                NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data1];        //将归档的数据全部写到data中,再将data中的数据写到文件中!        [archiver encodeObject:aDic forKey:@"Mydic"];//将字典中的内容归档                [archiver encodeObject:array forKey:@"Myarray"];//将数组中的内容归档                [archiver finishEncoding];                [data1 writeToFile:PATH atomically:YES];//将归档后的数据统一写入文件中        [aDic release];        [array release];        [data1 release];        [archiver release];                        //解归档        NSData *receiveData=[[NSData alloc]initWithContentsOfFile:PATH];        NSKeyedUnarchiver * unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:receiveData];//        NSDictionary *unarchiverAdic=[unarchiver decodeObjectForKey:@"Mydic"];        NSLog(@"%@",unarchiverAdic);        NSArray *unarchiverArray=[unarchiver decodeObjectForKey:@"Myarray"];        NSLog(@"%@",unarchiverArray);                //以上是对OC中已有的数据类型进行的操作,若对自定义的数据类型进行归档与解归档,则方法如下//1.头文件#import 
@interface Student : NSObject 
{  NSString* _name;  NSUInteger _age;    //注:若该类是一个复合的数据类型,则该类中拥有的非系统自带的类型也必须实现NSCoding协议,否则无法归档,例如,假设该类含有一个Dog *_dog 的对象,该类非系统自带,则该类也必须实现NSCoding协议}@property (copy,nonatomic) NSString* name;@property (assign,nonatomic) NSUInteger age;- (id)initWithName:(NSString*)aName andAge:(NSUInteger)aAge;  //NSCoding协议中要求实现的方法//- (void)encodeWithCoder:(NSCoder *)aCoder;//- (id)initWithCoder:(NSCoder *)aDecoder;@end//方法实现部分#import "Student.h"@implementation Student- (id)init{  return [self initWithName:@"张三丰" andAge:100];}- (NSString *)description{  return [NSString stringWithFormat:@"Name:%@ Age:%lu", self.name,self.age];}- (id)initWithName:(NSString*)aName andAge:(NSUInteger)aAge{  if (self = [super init]) {    _name = aName;    _age = aAge;  }  return self;}- (void)encodeWithCoder:(NSCoder *)aCoder{  [aCoder encodeObject:[self name] forKey:@"Name"];  [aCoder encodeInteger:(NSUInteger)self.age forKey:@"Age"];}- (id)initWithCoder:(NSCoder *)aDecoder{  if (self = [super init]) {    self.name = [aDecoder decodeObjectForKey:@"Name"];    self.age = (NSUInteger)[aDecoder decodeIntegerForKey:@"Age"];  }  return self;}@end3.main#import 
#import "Student.h"int main(int argc, const char * argv[]){  @autoreleasepool {#if 0      //系统类型的归档和解档操作    NSDictionary* aDic = [[NSDictionary alloc]initWithObjectsAndKeys:@"hello",@"one",@"1000phone",@"two", nil];    NSString* path = @"/Users/youDirector/Desktop/aDic.archive";      //系统类的对象归档    [NSKeyedArchiver archiveRootObject:aDic toFile:path];        NSDictionary* bDic = [NSKeyedUnarchiver unarchiveObjectWithFile:path];        NSLog(@"bDic:%@",bDic);    NSLog(@"aDic:%@",aDic);#endif        Student* aStu = [[Student alloc] init];    NSLog(@"aStu:%@",aStu);        NSString* path = @"/Users/bencai/Desktop/aStu.archive";        [NSKeyedArchiver archiveRootObject:aStu toFile:path];//自定类归档        Student* bStu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];//解归档    NSLog(@"bStu:%@",bStu);  }    return 0;}