点此下载iOS归档,持久化存储,解归档详细工程
//数据持久化的本质:将数据读取成文件保存在本地. 沙盒机制就是系统针对于每一个程序在本地生成的文件夹(名字随机生成), 对于不同的应用程序, 不能访问其他应用程序沙盒内的内容, 对于该应用程序内容起到保护作用:1 Documents:用来存储长久保存的数据 2 xxx.app:应用程序的包, 包含应用程序加载所需的所有资源(readonly只读, 不可修改), 平时使用的NSBundle就是该包 3 Library: 1) Caches:本地缓存, 存储想暂时保存的数据(Videos, Musics, Images) 比如:下载的视频, 音频, 图片都存储在该文件夹下 2) Preferences:存储用户的偏好设置, 比如程序是否是第一次启动 4 tmp:存储还未下载完的视频, 音频, 当下载完后, 将文件转移到Caches文件夹下
#import "WYLReadAndWriteViewController.h"
#import "WYLArchive.h"
@interface WYLReadAndWriteViewController ()<UITextFieldDelegate>
@end
@implementation WYLReadAndWriteViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"文件读写";
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 84, 220, 40)];
textField.tag = 100;
textField.placeholder = @"请输入内容";
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
[textField release];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(40, 174, 220, 40)];
textField2.tag = 101;
textField2.placeholder = @"显示上一个输入框的内容";
textField2.delegate = self;
textField2.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField2];
[textField2 release];
UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeSystem];
writeButton.frame = CGRectMake(45, 260, 60, 30);
[writeButton setTitle:@"写入" forState:UIControlStateNormal];
[writeButton addTarget:self action:@selector(write:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:writeButton];
UIButton *readButton = [UIButton buttonWithType:UIButtonTypeSystem];
readButton.frame = CGRectMake(190, 260, 60, 30);
[readButton setTitle:@"读取" forState:UIControlStateNormal];
[readButton addTarget:self action:@selector(read:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:readButton];
UIButton *push = [UIButton buttonWithType:UIButtonTypeSystem];
push.frame = CGRectMake(120, 310, 60, 30);
[push setTitle:@"push" forState:UIControlStateNormal];
[push addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:push];
}
- (NSString *)getFilePath
{
//用来获取指定文件夹的路径:<#NSSearchPathDirectory directory#>:指定的文件夹;<#NSSearchPathDomainMask domainMask#>:设置查找的域, 我们自己的文件都是存储在永华域的;<#BOOL expandTilde#>:是否使用详细路径(绝对路径) 因为最初该方法是使用与MAC OS下的, 而对于电脑系统来说, 可能会存储多个用户, 所以获取到得用户可能有多个, 所以返回值类型是数组, 但是对于iOS下, 就要只有一个用户, 所以数组中只有一个元素
/*
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//2)拼接上要存储文件的路径
NSString *newFilePath = [documentsPath stringByAppendingPathComponent:@"aa.txt"];
NSLog(@"%@", newFilePath);
*/
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [filePath stringByAppendingPathComponent:@"test.txt"];
NSLog(@"%@", newPath);
return newPath;
}
- (void)read:(UIButton *)button
{
//每次写入都会将之前的内容覆盖掉, 若想保留之前的数据, 需要讲之前的数据读出, 然后将要存储的数据拼接在一起, 一起存入
/*
NSString *newFilePath = [self getFilePath];
NSError *error = nil;
NSString *content = [NSString stringWithContentsOfFile:newFilePath encoding:NSUTF8StringEncoding error:&error];
UITextField *tf = (UITextField *)[self.view viewWithTag:101];
tf.text = content;
*/
//字符串从本地读取
/*
NSString *filePath = [self getFilePath];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
UITextField *tf = (UITextField *)[self.view viewWithTag:101];
tf.text = content;
*/
//数组从本地文件读取
NSString *filePath = [self getFilePath];
// NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
//从字典从本地读取
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
UITextField *tf = (UITextField *)[self.view viewWithTag:100];
UITextField *tf1 = (UITextField *)[self.view viewWithTag:101];
tf.text = dic[@"tf2"];
tf1.text = dic[@"tf1"];
}
//文件读写暂时只支持:NSString, NSArray, NSDictionary, NSData, 以及他们的子类.写入文件:writeToFile:(这是对象调用的方法), 读取文件:每一个类自带的能够根据路径创建对象的方法:[类名 类WithContentsOfFile]; 字符串:[NSString stringWithContentsOfFile], 数组:[NSArray arrayWithContentsOfFile], 字典:[NSDictionary dictionaryWithContentsOfFile], 二进制流:[NSData dataWithContentsOfFile],(牢牢谨记:对于数组, 字典这样的容器类, 内部的成员也必须是能够实现文件读写的八大类之一)
- (void)write:(UIButton *)button
{
//写入时, 将第一个输入框中的文字, 写入到本地文件
//1 获取存储的内容
UITextField *tf = (UITextField *)[self.view viewWithTag:100];
NSString *content = tf.text;
//2 获取到所要存储的文件路径
//1)获取Documents文件夹路径
NSString *newFilePath = [self getFilePath];
//3 将内容存储到指定文件路径
// NSError *error = nil;
//字符串写入本地文件
// BOOL isSucceed = [content writeToFile:newFilePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
//数组写入本地文件
UITextField *tf2 = (UITextField *)[self.view viewWithTag:101];
NSString *content1 = tf2.text;
// NSArray *arr = @[content, content1];
// BOOL isSucceed = [arr writeToFile:newFilePath atomically:YES];
//字典写入本地文件
NSDictionary *dic = @{@"tf1": content, @"tf2": content1};
BOOL isSucceed = [dic writeToFile:newFilePath atomically:YES];
NSLog(@"%d", isSucceed);
}
- (void)push:(UIButton *)button
{
WYLArchive *archivieVC = [[WYLArchive alloc]init];
[self.navigationController pushViewController:archivieVC animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
#import "WYLArchive.h"
#import "Person.h"
@interface WYLArchive ()<UITextFieldDelegate>
@end
@implementation WYLArchive
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"归档与反归档";
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(40, 84, 220, 40)];
textField.tag = 100;
textField.placeholder = @"请输入内容";
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
UITextField *textField2 = [[UITextField alloc]initWithFrame:CGRectMake(40, 174, 220, 40)];
textField2.tag = 101;
textField2.placeholder = @"显示上一个输入框的内容";
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.delegate = self;
[self.view addSubview:textField2];
[textField2 release];
UIButton *fileButton = [UIButton buttonWithType:UIButtonTypeSystem];
fileButton.frame = CGRectMake(45, 260, 60, 30);
[fileButton setTitle:@"归档" forState:UIControlStateNormal];
[fileButton addTarget:self action:@selector(file:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:fileButton];
UIButton *archiveButton = [UIButton buttonWithType:UIButtonTypeSystem];
archiveButton.frame = CGRectMake(190, 260, 60, 30);
[archiveButton setTitle:@"反归档" forState:UIControlStateNormal];
[archiveButton addTarget:self action:@selector(archive:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:archiveButton];
}
- (NSString *)getPath
{
//获得文件夹的路径
/*
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [filePath stringByAppendingPathComponent:@"archive"];
return newPath;
*/
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *newPath = [path stringByAppendingPathComponent:@"archiver"];
return newPath;
}
- (void)file:(UIButton *)button
{
//获取输入框的内容
UITextField *tf1 = (UITextField *)[self.view viewWithTag:100];
UITextField *tf2 = (UITextField *)[self.view viewWithTag:101];
/*
//封装成Person对象
Person *person = [[Person alloc] initWithName:tf1.text gender:tf2.text age:18];
//1 创建归档对象
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//2 归档
[archiver encodeObject:person forKey:@"person"];
[person release];
//3 结束归档, 当结束归档之后, 再归档无效
[archiver finishEncoding];
[archiver release];
//4 data写入文件
[data writeToFile:[self getPath] atomically:YES];
*/
Person *person = [[Person alloc] initWithName:tf1.text gender:tf2.text age:18];
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:person forKey:@"archiver"];
[person release];
[archiver finishEncoding];
[archiver release];
[data writeToFile:[self getPath] atomically:YES];
}
- (void)archive:(UIButton *)button
{
/*
//1 初始化NSMutableData对象
NSMutableData *data = [NSMutableData dataWithContentsOfFile:[self getPath]];
//2 创建一个反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//3 反归档
Person *person = [unarchiver decodeObjectForKey:@"person"];
//4 结束反归档
[unarchiver finishDecoding];
[unarchiver release];
*/
NSMutableData *data = [NSMutableData dataWithContentsOfFile:[self getPath]];
NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
Person *person = [unarchive decodeObjectForKey:@"archiver"];
[unarchive finishDecoding];
[unarchive release];
UITextField *tf1 = (UITextField *)[self.view viewWithTag:100];
UITextField *tf2 = (UITextField *)[self.view viewWithTag:101];
tf1.text = person.gender;
tf2.text = person.name;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end