沙盒介紹
Documents:用于存儲(chǔ)用戶數(shù)據(jù)躲查,可被iTunes備份
Library:包含兩個(gè)子目錄
可創(chuàng)建子文件夾它浅。可以用來放置您希望被備份但不希望被用戶看到的數(shù)據(jù)镣煮。該路徑下的文件夾姐霍,除Caches以外,都會(huì)被iTunes備份。
Caches:適合存儲(chǔ)體積大镊折,不需要備份的非重要數(shù)據(jù)
Preferences:通常保存應(yīng)用的設(shè)置信息(NSUserDefaults)
tmp:用于存放臨時(shí)文件胯府,不會(huì)被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存儲(chǔ)
特點(diǎn):只能存儲(chǔ)OC常用數(shù)據(jù)類型(NSString、NSDictionary恨胚、NSArray骂因、NSData、NSNumber等類型)而不能直接存儲(chǔ)自定義模型對(duì)象与纽。
- plist存儲(chǔ)
//2.1 plist文件的創(chuàng)建與刪除
NSFileManager *manager = [NSFileManager defaultManager];
// 獲取文件路徑 Documents
NSString *plistPath = PATH_AT_Document(@"tst.plist");
// 開始創(chuàng)建文件
[manager createFileAtPath:plistPath contents:nil attributes:nil];
// 移除文件
//[manager removeItemAtPath:plistPath error:nil];
//2.2 plist 存儲(chǔ)/讀取字典數(shù)據(jù)
// 存儲(chǔ)
NSDictionary *writeDic = @{
@"a": @"aaa",
@"b": @"bbb",
@"c": @"ccc",
@"d": @"ddd"
};
[writeDic writeToFile:plistPath atomically:YES];
// 讀取
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"plist - 讀取字典數(shù)據(jù):%@", readDic);
//2.3 plist 存儲(chǔ)/讀取數(shù)組數(shù)據(jù)
NSArray *writeArray = @[@"a", @"b", @"c", @"d", @"e"];
// 存儲(chǔ)
[writeArray writeToFile:plistPath atomically:YES];
// 讀取
NSArray *readArray = [NSArray arrayWithContentsOfFile:plistPath];
NSLog(@"plist - 讀取數(shù)組數(shù)據(jù): %@", readArray);