沙盒介绍
Documents:用于存储用户数据,可被iTunes备份
Library:包含两个子目录
可创建子文件夹。可以用来放置您希望被备份但不希望被用户看到的数据。该路径下的文件夹,除Caches以外,都会被iTunes备份。
Caches:适合存储体积大,不需要备份的非重要数据
Preferences:通常保存应用的设置信息(NSUserDefaults)
tmp:用于存放临时文件,不会被iTunes备份
- 1.获取文件目录的路径
//document文件夹路径
#define PATH_AT_Document(name) [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
//temp文件夹路径
#define PATH_AT_Tmp(name) [NSTemporaryDirectory() stringByAppendingPathComponent:name]
//cache文件夹路径
#define PATH_AT_Cache(name) [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
//Libary文件夹路径
#define PATH_AT_Library(name) [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:name]
- plist存储
特点:只能存储OC常用数据类型(NSString、NSDictionary、NSArray、NSData、NSNumber等类型)而不能直接存储自定义模型对象。
- plist存储
//2.1 plist文件的创建与删除
NSFileManager *manager = [NSFileManager defaultManager];
// 获取文件路径 Documents
NSString *plistPath = PATH_AT_Document(@"tst.plist");
// 开始创建文件
[manager createFileAtPath:plistPath contents:nil attributes:nil];
// 移除文件
//[manager removeItemAtPath:plistPath error:nil];
//2.2 plist 存储/读取字典数据
// 存储
NSDictionary *writeDic = @{
@"a": @"aaa",
@"b": @"bbb",
@"c": @"ccc",
@"d": @"ddd"
};
[writeDic writeToFile:plistPath atomically:YES];
// 读取
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"plist - 读取字典数据:%@", readDic);
//2.3 plist 存储/读取数组数据
NSArray *writeArray = @[@"a", @"b", @"c", @"d", @"e"];
// 存储
[writeArray writeToFile:plistPath atomically:YES];
// 读取
NSArray *readArray = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"plist - 读取数组数据: %@", readArray);