iOS 设计中数据持久化—归档NSKeyedArchiver与解档NSKeyedUnarchiver的方法
2016-03-21 18:48
465 查看
思路:先创建一些数据文件 ,之后先对创建的数据文件使用归档方法(函数方法 [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
代码实现如下:
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
相关文章推荐
- android、IOS 基于webview 与 HTML 的集成
- iOS的layout机制相关方法
- IOS开发-ObjC-对象、封装
- duplicate symbols for architecture x86_64原因及解决办法
- iOS数据 - Core Data使用
- IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、@synthesize、@property、@dyna
- iOS 判断第一个字符是数字还是汉字
- iOS 懒加载
- iOS中正则表达式的使用
- iOS 基础之NSArray数组去重
- iOS沙盒 -- 文件对接器(NSFileHandle)
- iOS知识 ----加载显示gif图片
- iOS开发中 修改状态栏颜色 或者隐藏状态栏颜色
- iOS开发的经典博客和文章汇总
- iOS 自定义的对象类型的解档和归档
- iOS-设计模式
- iOS 数据的归档(NSKeyedArchiver )与数据的解档(NSKeyedUnarchiver) 加密 提高安全性
- iOS udid+keychain 唯一标示
- IOS平台开发实战培训
- iOS小知识