不积跬步,无以至千里
不积小流,无以成江海
目录结构
默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的 时候会包括此目录 Library:存储程序的默认设置或其它状态信息; Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除 tmp:提供一个即时创建临时文件的地方
获取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目录,不能按照上面的做法获得目录了,有个函数可以获得应用的根目录:
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;
}