不積跬步,無以至千里
不積小流,無以成江海
目錄結(jié)構(gòu)
默認(rèn)情況下府瞄,每個(gè)沙盒含有3個(gè)文件夾:Documents, Library 和 tmp航罗。因?yàn)閼?yīng)用的沙盒機(jī)制哪审,應(yīng)用只能在幾個(gè)目錄下讀寫文件
Documents:蘋果建議將程序中建立的或在程序中瀏覽到的文件數(shù)據(jù)保存在該目錄下娶牌,iTunes備份和恢復(fù)的 時(shí)候會包括此目錄 Library:存儲程序的默認(rèn)設(shè)置或其它狀態(tài)信息; Library/Caches:存放緩存文件啼辣,iTunes不會備份此目錄啊研,此目錄下文件不會在應(yīng)用退出刪除 tmp:提供一個(gè)即時(shí)創(chuàng)建臨時(shí)文件的地方
獲取Documents
- (NSString *)documentsPath {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
}
/Users/zpl/Library/Developer/CoreSimulator/Devices/3DBEB291-03AA-4F2B-B95B-B7130505752B/data/Containers/Data/Application/2562EA66-2B85-4227-9A64-41C98FE493D9/Documents
- 獲取Caches
- (NSString *)CachesPath {
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
/Users/zpl/Library/Developer/CoreSimulator/Devices/3DBEB291-03AA-4F2B-B95B-B7130505752B/data/Containers/Data/Application/2562EA66-2B85-4227-9A64-41C98FE493D9/Library/Caches
- 獲取tmp
- (NSString *)tmpPath {
//使用NSSearchPathForDirectoriesInDomains只能定位Caches目錄和Documents目錄
//tmp目錄御滩,不能按照上面的做法獲得目錄了鸥拧,有個(gè)函數(shù)可以獲得應(yīng)用的根目錄:
NSString *fileName=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
return fileName;
}
/Users/zpl/Library/Developer/CoreSimulator/Devices/3DBEB291-03AA-4F2B-B95B-B7130505752B/data/Containers/Data/Application/FA35DF7D-56F5-445E-B6BF-8A22190565DF/tmp
- 添加名字后綴
+ (NSString *)filePathWithFileName:(NSString *)fileName
{
// NSString *filePath = [[self documentsPath]stringByAppendingPathComponent:fileName];
NSString *CachesPath = [[self CachesPath]stringByAppendingPathComponent:fileName];
return filePath;
}