归档

1.流程:

中介:NSMutableData(必须是新对象,里面不能有数据)

  • 存档-》编码-》写入路径。
  • 读取路径-》解档-》解码。

2. 可以 对集合、NSData、对象属性归档(写到**.xml本地文件)。

3.对象属性的"编码解码(类似kvc)":实现NSCoding协议的2个方法,存档、读取路径时自动调用。

-(void)encodeWithCoder:(NSCoder *)aCoder
-(id)initWithCoder:(NSCoder *)aDecoder

 Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject <NSCoding>
{
    NSString *_name;
    int _age;
}
@property(nonatomic,retain)NSString *name;
@property(nonatomic,assign)int age;
@end

Student.m

#import "Student.h"

@implementation Student
@synthesize name = _name,age = _age;

-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_name  forKey:@"name"];
    [aCoder encodeInteger:_age forKey:@"age"];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self)
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}

@end

AppDelegate.m

    NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
    //[arr writeToFile:PATH atomically:YES];
   //NSArray *arra = [NSArray arrayWithContentsOfFile:PATH]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil]; NSMutableData *mData = [[NSMutableData alloc]init]; //归档 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];//存档 [archiver encodeObject:arr forKey:@"arr"]; [archiver encodeObject:dic forKey:@"dic"]; [archiver finishEncoding]; [mData writeToFile:PATH atomically:YES];//写入路径 //解档 NSData *data = [NSData dataWithContentsOfFile:PATH];//读取路径 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];//解档 NSDictionary *dictionary = [unarchiver decodeObjectForKey:@"dic"]; NSLog(@"%@",dictionary); //2.存对象属性 Student *s = [[Student alloc]init]; s.name = @"jobs"; s.age = 25; NSData * data1 = [NSKeyedArchiver archivedDataWithRootObject:s]; [data1 writeToFile:@"/Users/huen/Desktop/归档解档/p.rtf" atomically:YES]; NSData *data2 = [NSData dataWithContentsOfFile:@"/Users/huen/Desktop/归档解档/p.rtf"]; Student *stu = [NSKeyedUnarchiver unarchiveObjectWithData:data2]; NSLog(@"%@,%d",stu.name,stu.age);

 .

 Student *s = [[Student alloc]init];
    s.name = @"jobs";
    s.age = 25;
    
    Student *s2 = [[Student alloc]init];
    s2.name = @"apple";
    s2.age = 23;
    
    Student *s3 = [[Student alloc]init];
    s3.name = @"bill";
    s3.age = 22;
    
    NSArray *students = @[s,s2,s3];
    NSMutableData *mData = [NSMutableData new];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    [archiver encodeObject:students forKey:@"studentKey"];
    [archiver finishEncoding];
    [mData writeToFile:path atomically:YES];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSKeyedUnarchiver *unarcher = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    NSArray *list = [unarcher decodeObjectForKey:@"studentKey"];
    [unarcher finishDecoding];
    NSLog(@"__________jjjjjjjjj=%@",list);

 

posted @ 2014-01-27 10:22  forrHuen  阅读(208)  评论(0)    收藏  举报