三大目录:
应用程序沙盒目录下有三个文件夹Documents、Library(下面有Caches和Preferences目录)、tmp。
- Documents:保存应用运行时生成的需要持久化的数据iTunes会自动备份该目录。苹果建议将在应用程序中浏览到的文件数据保存在该目录下。
- Library/Caches:一般存储的是缓存文件,例如图片视频等,此目录下的文件不会再应用程序退出时删除,在手机备份的时候,iTunes不会备份该目录。
- Library/Preferences:保存应用程序的所有偏好设置iOS的Settings(设置),我们不应该直接在这里创建文件,而是需要通过NSUserDefault这个类来访问应用程序的偏好设置。iTunes会自动备份该文件目录下的内容。
- tmp:临时文件目录,在程序重新运行的时候,和开机的时候,会清空tmp文件夹。
简单来说:
1.Documents : 存储长久保存的数据 不建议存储大文件 (iTunes会自动备份该目录)
2.library:
Caches:存放的缓存,比如:视频,音频,图片,小说等等 有时候它来保存应用程序再次启动过程中需要的信息。几乎所有的第三方框架的缓存信息处理都在这个文件中,一般的大容量文件都放在这里 (iTunes不会自动备份该目录)
Perferences:存储偏好设置,比如:应用程序是否是第一次启动、保存用户名和密码.我们最常用这个 (iTunes会自动备份该目录)
3.tmp:存储临时文件,比如:下载的zip包,解压后直接删除.(iTunes不会自动备份该目录,重启开机都会自动清空)
如图:
获取方式
//documents 路径
// 该文件夹 一般存储用户的一些数据
NSArray *documentsPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *document = [documentsPathArr lastObject];
NSLog(@"%@", document);
// Caches缓存文件夹路径
// 该文件夹 一般存储缓存文件
NSArray *cachesPathArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPath = cachesPathArr[0];
NSLog(@"%@", cachesPath);
// 打印tmp文件夹
// 该文件夹 一般存储临时文件
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"%@", tempPath);
// 打印沙盒的主目录路径
NSLog(@"%@", NSHomeDirectory());
//Preferences
一般无需创建,需要我们应用nsuserdefault间接访问存取操作
数据的存储方式:
1、NSUserDefaults类 (字典、数组、oc基本数据类型一些比较小的数据 但是不支持对象存储)
NSMutableArray *mutArr = [[NSMutableArray alloc]initWithObjects:@"1", nil];
//存入数组并同步
[[NSUserDefaults standardUserDefaults] setObject:mutArr forKey:@"mutableArr"];
[[NSUserDefaults standardUserDefaults] synchronize];
//读取存入的数组 打印
NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"mutableArr"];
NSLog(@"%@",arr);
2、Plist文件 (项目中的plist文件一般作为固态的、xcode中可视化数据形势保存,对于经常需要改动的数据就不好操作了 依旧不能存储对象)
//获取plist文件内容
NSString *string = [[NSBundle mainBundle] pathForResource:@"testPlist" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:string];
NSLog(@"%@",dic); //打印文件中的内容
//写入plist文件内容
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *plistpath = [paths objectAtIndex:0];
NSLog(@"path = %@",plistpath);
NSString *filename=[plistpath stringByAppendingPathComponent:@"testPlist.plist"];
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
//写入内容
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"test",nil];
[dic writeToFile:filename atomically:YES];
//读文件
NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"dic is:%@",dic2);
3、解归档 可以存储对象 但是要遵循解归档协议
========================.h===========================
@interface TestModel : NSObject <NSCoding> //解归档需要遵循Nscoding协议,并实现相关方法
@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) NSString *sex;
@end
============================.m========================
import "TestModel.h"
define Name @"name"
define Age @"age"
define Sex @"sex"
@implementation TestModel
//需要实现NSCoding中的协议的两个方法
-
(id)initWithCoder:(NSCoder *)aDecoder{
if (self == [super init]) {
self.name = [aDecoder decodeObjectForKey:Name]; self.sex = [aDecoder decodeObjectForKey:Sex]; self.age = [[aDecoder decodeObjectForKey:Age] integerValue];
}
return self;
}
-
(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:Name];
[aCoder encodeObject:self.sex forKey:Sex];
[aCoder encodeObject:[NSNumber numberWithInteger:self.age] forKey:Age];
}
-
(NSString *)description{
return [NSString stringWithFormat:@"%@--%@--%ld岁",self.name,self.sex,(long)self.age];
}
@end
===============================操作=============================
//创建对象 并赋值
TestModel *model = [[TestModel alloc]init];
model.name = @"小明";
model.age = 25;
model.sex = @"man";
//归档
NSMutableData *data = [[NSMutableData alloc] init];
//创建归档辅助类
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//编码
[archiver encodeObject:model forKey:@"model"];
//结束编码
[archiver finishEncoding];
//写入到沙盒
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [array.firstObject stringByAppendingPathComponent:@"archiverModel"];
if([data writeToFile:fileName atomically:YES]){
NSLog(@"归档成功");
}
//解档
NSData *undata = [[NSData alloc] initWithContentsOfFile:fileName];
//解档辅助类
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:undata];
//解码并解档出model
TestModel *unModel = [unarchiver decodeObjectForKey:@"model"];
NSLog(@"%@",unModel);
//关闭解档
[unarchiver finishDecoding];
4、手动创建文件存放沙盒( 沙盒中只能保存OC中的基本数据,自定义的对象不能直接存入)
// 1,获取家目录路径的函数:
// NSString *homeDir = NSHomeDirectory();
// 2,获取Documents目录路径的方法:
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *docDir = [paths objectAtIndex:0];
// 3,获取Caches目录路径的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
// 4,获取tmp目录路径的方法:
// NSString *tmpDir = NSTemporaryDirectory();
=================操作===================================
//假设我们需往cache 存入数据,并命名为test的txt格式文件中 写入
NSString *filePath = [cachesDir stringByAppendingPathComponent:@"test.txt"];
NSArray *dic = [[NSArray alloc] initWithObjects:@"test",@"test1" ,nil];
if([dic writeToFile:filePath atomically:YES]){
NSLog(@"存入成功");
}
//取出数据 打印 取出
NSLog(@"%@",[NSArray arrayWithContentsOfFile:filePath]);
5、sqlite
数据库的优点:
该方案可以存储大量的数据,存储和检索的速度非常快.
能对数据进行大量的聚合,这样比起使用对象来讲操作要快.
数据库的缺点:
它没有提供数据库的创建方式
它的底层是基于C语言框架设计的, 没有面向对象的API, 用起来非常麻烦
-
发杂的数据模型的数据建表,非常麻烦
在实际开发中我们都是使用的是FMDB第三方开源的数据库,该数据库是基于splite封装的面向对象的框架.
6、CoreData
coreData是苹果官方在iOS5之后推出的综合性数据库,其使用了对象关系映射技术,将对象转换成数据,将数据存储在本地的数据库中
coreData为了提高效率,需要将数据存储在不同的数据库中,比如:在使用的时候,最好是将本地的数据保存到内存中,这样的目的是访问速度比较快.
CoreData与SQLite进行对比
SQLite
1、基于C接口,需要使用SQL语句,代码繁琐
2、在处理大量数据时,表关系更直观
3、在OC中不是可视化,不易理解
CoreData
1、可视化,且具有undo/redo能力
2、可以实现多种文件格式:
* NSSQLiteStoreType
* NSBinaryStoreType
* NSInMemoryStoreType
* NSXMLStoreTyp
3、苹果官方API支持,与iOS结合更紧密
具体实现操作可以参考https://www.jianshu.com/p/cd20f4a06c9d