iOS 设计中数据持久化—归档NSKeyedArchiver与解档NSKeyedUnarchiver的方法

思路:先创建一些数据文件 ,之后先对创建的数据文件使用归档方法(函数方法 [NSKeyedArchiver archiveRootObject:归档内容 toFile:存储路径];)进行归档,检查归档成功与否,若成功,之后再按解档方法(函数方法 [NSKeyedUnarchiver unarchiveObjectWithFile:解档路径];)进行解档
 
 
代码实现如下:
ViewController.h
 
 
#import <UIKit/UIKit.h>
#define Path [NSHomeDirectory() stringByAppendingPathComponent:@"test.src]//宏定义 归档路径
@interface ViewController : UIViewController
//控制归档方法的按钮
@property(strong,nonatomic)UIButton *Buttonarchiver;
//控制解档方法的按钮
@property(strong,nonatomic)UIButton *ButtonUnarchiver;
@end
 
ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",Path);
    //归档
    NSArray *arr=@[@"AA",@"BB",@"CC"];
    //判断归档成功与否
   BOOL bol= [NSKeyedArchiver archiveRootObject:arr toFile:Path];
   NSLog(@"归档成功与否=%d",bol);
 
    //创建控制归档方法的按钮
    self.Buttonarchiver=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
    self.Buttonarchiver.backgroundColor=[UIColor redColor];
    [self.Buttonarchiver setTitle:@"归档" forState:UIControlStateNormal];
   //调用方法
    [self.Buttonarchiver addTarget:self action:@selector(dataArchiver) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.Buttonarchiver];
   
    //创建控制解档方法的按钮
    self.ButtonUnarchiver=[[UIButton alloc]initWithFrame:CGRectMake(200, 100, 50, 50)];
    self.ButtonUnarchiver.backgroundColor=[UIColor redColor];
    [self.ButtonUnarchiver setTitle:@"解档" forState:UIControlStateNormal];
     //调用方法
    [self.ButtonUnarchiver addTarget:self action:@selector(dataUnArchiver) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.ButtonUnarchiver];
 
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //获取归档的内容
 NSArray *array=[NSKeyedUnarchiver unarchiveObjectWithFile:Path];
    NSLog(@"%@",array);

}

//归档方法
-(void)dataArchiver
{
    //初始化需要归档的内容
    NSArray *array=@[@"qq",@"ww",@"yy"];
    NSString *str=@"lamco.com.cn";
    BOOL bol=YES;
    //初始化可变data
    NSMutableData *data=[NSMutableData  data];
    //保存data到KeyedArchiver
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
   
    //存档方式
    [archiver encodeObject:array forKey:@"array"];
    [archiver encodeObject:str forKey:@"str"];
    [archiver encodeBool:bol forKey:@"bol" ];
    //完成存档
    [archiver finishEncoding];
    //查看存档后的内容
    NSLog(@"archiver=%@",archiver);
    BOOL bol1=[data writeToFile:Path atomically:YES];
    NSLog(@"%d",bol1);
}
//解档方法
-(void)dataUnArchiver
{
    //获取文件路径
    NSData *data=[NSData dataWithContentsOfFile:Path];
    //读取路径中的内容
    NSKeyedUnarchiver *keyunarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    //以关键字获取解档的内容
    NSArray *testArray=[ keyunarchiver decodeObjectForKey:@"bol"];
    NSLog(@"testArray=%@",testArray);

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
 

@end

posted @ 2016-03-21 18:48  右手指尖轻轻触  阅读(318)  评论(0编辑  收藏  举报