在开始之前,我需要强调一下这样一个事实,即我查看了处理文档目录的每个帖子。
所以我会尝试解决我的问题,以便更好地帮助你。
我正在开发一个针对5.1的iOS应用程序。我正在使用XCode 4.4.1和iOS模拟器版本5.1(272.21)。
据我了解,当在模拟器中安装应用程序时,其目录结构将映射到
下/Library/Application Support/iPhone Simulator/[IOS_VERSION]/Applications/[APP_UUID]
运行我的应用程序时会正确反映这一点。
此外,我能够使用以下代码
成功创建和使用临时目录NSString *tmpDir = NSTemporaryDirectory();
导致以下路径
/Library/Application Support/iPhone Simulator/[IOS_VERSION]/Applications/[APP_UUID]/tmp
当我想使用应该位于
的 Documents 目录时,问题就开始出现了/Library/Application Support/iPhone Simulator/[IOS_VERSION]/Applications/[APP_UUID]/Documents
以下代码检查该路径是否存在,然后使用NSLog记录它,即使它表明存在导航到该位置也会返回找不到的文件。
+ (NSString *) currentPath{
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
searchPaths=nil;
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:documentPath];
if (fileExists == TRUE) {
NSLog(@" %@ already exists",documentPath);
} else {
NSLog(@"doesn't exists");
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if(![fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:true attributes:nil error:&error])
{
NSLog(@"Couldn't create documents directory %@",error);
}
}
return documentPath;
}
NSLog系列的结果是:
2012-08-09 23:12:09.813 AMM[22656:c07] /Users/fotis/Library/Application Support/iPhone Simulator/5.1/Applications/7CE8645A-BDD7-4AB6-8CAB-B0EF1579CD2B/Documents already exists
在终端
> pwd
/Users/fotis/Library/Application Support/iPhone Simulator/5.1/Applications/7CE8645A-BDD7-4AB6-8CAB-B0EF1579CD2B
>ls -lsa
total 0
0 drwxr-xr-x 5 fotis 170 Aug 9 23:12 .
0 drwxr-xr-x 3 fotis 102 Aug 9 22:50 ..
0 drwxr-xr-x 30 fotis 1020 Aug 9 23:12 AMM.app
0 drwxr-xr-x 4 fotis 136 Aug 9 22:50 Library
0 drwxr-xr-x 4 fotis 136 Aug 9 22:51 tmp
如您所见,我的 Documents ghost目录不存在。对于我的生活,我无法理解它背后的魔力。需要注意的一点是,我在我的app委托的“-didFinishLaunchingWithOptions”方法中运行它,因为我在那里做了一些初始化。
有什么想法吗?
答案 0 :(得分:0)
模拟器在那个场地上有点笨拙,过去你的应用程序没有在模拟器上运行时甚至找不到模拟的沙盒。无论如何,您不必担心创建Documents文件夹,它将始终存在于设备上(还有模拟器,因为您没有尝试在应用程序内或在Finder中删除它)
答案 1 :(得分:0)
我不知道我是否可以投票我自己的问题,但这是烤架。
事实证明,我的初始化过程的一部分是删除 Documents 目录中的某些文件。
因此,对于想知道缺少路径的人,请确保您的斜杠“/”是正确的,并且您的globs(*)DO NOT
包含您的根(Documents /)文件夹。