//
// ViewController.m
// 数据持久化
//
// Created by dllo on 15/8/19.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
///苹果手机为了保证自己数据上的绝对安全,设计了沙盒文件,每一个程序都配备了自己的沙盒文件,每一次运行,文件夹名字就会变成一个没有任何规律的字符串
///一个参数,,当前前往那一个文件夹,,前往document文件夹用NSDocumentDictionary..64行,,可以前往caches文件夹 68行...
///第二个参数,访问的文件夹类型,,,制定访问的是用户名文件夹
///第三个参数,,绝对路径[yes],相对路径[no].
//绝对路径是给系统来使用的,系统可以根据当前的路径找到文件夹,,我们在操作文件的时候都是用的绝对路径,,
//相对路径只会把要前往的文件夹显示,,其他部分都是~,,告诉程序员要去哪个文件夹
/// NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
// NSLog(@"%@",sandBox);
//沙盒里一共有三个文件
//1.是Document文件,,,:主要是用来存储用户想要存储的一些信息,,比如收藏的信息或者自己设置的一些内容,,所以我们做收藏功能就是前往这个文件夹里写文件...
//2.Library文件夹了是方便程序员使用的,,主要操作他里面的两个文件夹,, caches和Preferences..
//caches用来保存缓存的文件,,SDWebImage会把图片添加到缓存文件夹,清除缓存功能就是删除这个文件夹
//Preferences一般用来保存程序员设置的信息,,,比如NSUserDefaults 就会把数据保存在这个文件夹
//3.tmp文件:一般存放临时内容
//之前在沙盒里还有一个.app文件..在新版本里已经被移走了..
//ba简单的对象写入到本地 NSString..NSArry,,
//1.先通过数组获取沙盒路径,,
/// NSArray *sandBox1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//从数组中获取沙盒路径.
///NSString *sandboxPath=sandBox1[0];
//要给写入的文件拼接一个路径,拼接方式有两种..
//第一种..要自己添加斜杠
//NSString *documentPath=[sandboxPath stringByAppendingString:@"/顾宇.txt"];
//第二种,,自带斜杠
///NSString *documentPath=[sandboxPath stringByAppendingPathComponent:@"顾宇.plist"];
/NSLog(@"%@",documentPath);
///NSString *str=@"kdfglkj.,.,.gfhfdhdfghghgfhfghfghfghfggfgfghghgh";
//把字符串写入到本地
//第一个参数:文件要保存的路径
//第二个参数;对文件进行保护,,yes
//第三个参数;编码
//第四个参数;错误信息
/// [str writeToFile:documentPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//如果路径下有对应的文件,,则会把原来的文件覆盖,,如果没有,则会创建一个新的文件
//把沙盒文件读出来
// NSString *tempStr=[NSString stringWithContentsOfFile:documentPath encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@",tempStr);
///把数组写入到本地
// NSArray *sandBox=@[@"1",@"2",@"3",@"4"];
// //通过数组,,获取沙盒地址..
// NSArray *sandBox1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// //用字符串保存沙盒路径
// NSString *documentPath=sandBox1[0];
// //给要写入的文件拼接路径
// NSString *documentPath1=[documentPath stringByAppendingPathComponent:@"sadfsdf.txt" ];
// [sandBox writeToFile:documentPath1 atomically:YES];
// NSLog(@"%@",documentPath1);
// //把数组读出来
// NSArray *temp=[NSArray arrayWithContentsOfFile:documentPath1];
// NSLog(@"%@",temp);
///字典写入到本地
// NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"2",@"3",@"4", nil];
// NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentPth=sandBox[0];
// NSString *documentPth1=[documentPth stringByAppendingPathComponent:@"经费靠大家观看.txt"];
// [dic writeToFile:documentPth1 atomically:YES];
// NSLog(@"%@",documentPth1);
// //将字典打印
// NSDictionary *dic1=[NSDictionary dictionaryWithContentsOfFile:documentPth1];
// NSLog(@"%@",dic1);
///复杂对象写入到本地,主要指我们自己创建的对象写入到本地
//创建一个student对象
// Student *stu=[Student stuWithName:@"安逸臣" stuSex:@"男" stuAge:@"25" stuHobby:@"打球"];
// //1.通过数组获取沙盒路径
// NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// //2.用字符串接受沙盒路径
// NSString *sandPath=sandBox[0];
// //3.拼接文件夹路径,这个文件的扩展名是任意的
// NSString *documentPath=[sandPath stringByAppendingPathComponent:@"学生.avi"];
// ///对对象进行归档操作
// //第一个参数:要实施归档的对象
// //第二个参数:路径
// [NSKeyedArchiver archiveRootObject:stu toFile:documentPath];
// NSLog(@"%@",documentPath);
// //反归档
// Student *newStu=[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
// NSLog(@"%@",newStu.name);
///创建三个学生
Student *stu1=[Student stuWithName:@"安逸岑" stuSex:@"男" stuAge:@"23" stuHobby:@"玩篮球"];
Student *stu2=[Student stuWithName:@"宁致远" stuSex:@"男" stuAge:@"34" stuHobby:@"踢足球"];
Student *stu3=[Student stuWithName:@"文世轩" stuSex:@"男" stuAge:@"33" stuHobby:@"跳芭蕾"];
NSArray *arr=@[stu1,stu2,stu3];
//归档和反归档操作
NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandBoxPath=sandBox[0];
NSString *documentPath=[sandBoxPath stringByAppendingString:@"student.plist"];
[NSKeyedArchiver archiveRootObject:arr toFile:documentPath];
NSLog(@"%@",documentPath);
//反归档,,遍历学生的姓名
NSArray *stuArr=[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
for (Student *tempStu in stuArr) {
NSLog(@"%@",tempStu.name);
}
// for(NSInteger i=0;i<stuArr.count;i++){
// Student *stu=stuArr[i];
// NSLog(@"%@",stu.name);
// }
///NSUserDefaults 一般存放的是小数据,,比如字符串,,他的用法和字典类似
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:@"123456" forKey:@"password"];
NSArray *arr1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",arr1);
NSLog(@"%@",[defaults objectForKey:@"password"]);
通过文件管理者对文件夹进行操作
///在document文件夹下创建一个新的文件夹
NSArray *sandArr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *sandStr=sandArr[0];
//创建一个文件管理者
NSFileManager *manager=[NSFileManager defaultManager];
//给要创建的文件夹拼接一个路径
NSString *newStr=[sandStr stringByAppendingPathComponent:@"顾宇"];
//文件的名字不需要任何扩展名
//通过manager进行文件夹的创建
[manager createDirectoryAtPath:newStr withIntermediateDirectories:YES attributes:nil error:nil];
NSLog(@"%@",newStr);
//向新建的文件夹里写入字符串
NSString *guyu=@"顾宇";
NSString *guyustr=[newStr stringByAppendingPathComponent:@"guyu.txt"];
[guyu writeToFile:guyustr atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",guyustr);
///移除文件夹
[manager removeItemAtPath:guyustr error:nil];
///移除cache清除缓存
NSArray *cacheArr=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSLog(@"%@",cacheArr);
NSString *cacheStr=cacheArr[0];
[manager removeItemAtPath:cacheStr error:nil];
}
#pragma mark 总结:数据的持久化的步骤
//1.指定前往哪一个文件夹
//2.用字符串接受路径
//3.拼接文件路径
//4.写入本地或者归档操作
//注:如果是复杂对象归档,要签订nscoding协议,,并实现两个协议方法,,放在数组里的复杂对象归档也要签订协议
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// Student.h
// 数据持久化
//
// Created by dllo on 15/8/19.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
#pragma mark 如果想要实现归档和反归档,,需要先签订一个协议NSCoding
@interface Student : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *age;
@property(nonatomic,copy)NSString *hobby;
//针对四条属性写一个自定义的初始化方法和构造器
- (id)initWithstuName:(NSString *)name
stuSex:(NSString *)sex
stuAge:(NSString *)age
stuHobby:(NSString *)hobby;
+(Student *)stuWithName:(NSString *)name
stuSex:(NSString *)sex
stuAge:(NSString *)age
stuHobby:(NSString *)hobby;
@end
//
// Student.m
// 数据持久化
//
// Created by dllo on 15/8/19.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "Student.h"
@implementation Student
- (id)initWithstuName:(NSString *)name
stuSex:(NSString *)sex
stuAge:(NSString *)age
stuHobby:(NSString *)hobby{
self=[super init];
if (self) {
self.name=name;
self.sex=sex;
self.age=age;
self.hobby=hobby;
}
return self;
}
+(Student *)stuWithName:(NSString *)name
stuSex:(NSString *)sex
stuAge:(NSString *)age
stuHobby:(NSString *)hobby{
Student *stu=[[Student alloc]initWithstuName:name stuSex:sex stuAge:age stuHobby:hobby];
return stu;
}
#pragma mark 签订玩NSCoding协议之后,,需要实现两个协议方法,,一个是归档时候使用,,另一个是反归档时候使用
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_name forKey:@"姓名"];
[aCoder encodeObject:_sex forKey:@"性别"];
[aCoder encodeObject:_age forKey:@"年龄"];
[aCoder encodeObject:_hobby forKey:@"爱好"];
//使用encode方法要和数据的类型相匹配
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self=[super init];
if (self) {
//把数据根据之前的key反编译出来
self.name=[aDecoder decodeObjectForKey:@"姓名"];
self.age=[aDecoder decodeObjectForKey:@"年龄"];
self.sex=[aDecoder decodeObjectForKey: @"性别"];
self.hobby=[aDecoder decodeObjectForKey:@"爱好"];
}
return self;
}
@end
AppDelegate.h// UI16_数据持久化 Created by dllo on 15/8/19.// Copyright (c) 2015年 flg. All rights reserved.//#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end