我在我应用程序的文档文件夹中的 viewDidLoad
中创建了一个目录。
NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.projectsLocalPath = [localDirectory stringByAppendingPathComponent:@"Projects"];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.projectsLocalPath] == NO) {
[[NSFileManager defaultManager] createDirectoryAtPath:self.projectsLocalPath withIntermediateDirectories:YES attributes:nil error:nil];
}
之后,我列出所有文件,以便它们出现在日志中,检查它是否是一个目录:
//----- LIST ALL FILES -----
NSArray *contentOfFolder = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:localDirectory error:nil];
for (NSString *aPath in contentOfFolder) {
NSString *fullPath = [localDirectory stringByAppendingPathComponent:aPath];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) {
NSLog(@"%@ - IT IS A DIR", fullPath);
}
else {
NSLog(@"%@ - IT IS NOT A DIR", fullPath);
}
}
当我第一次运行该应用程序时,日志文件列表如下所示:
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-shm - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-wal - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Projects - IT IS A DIR
一切都是正确的。但以下运行在日志中显示 Projects
文件夹不再是目录。
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-shm - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Project.sqlite-wal - IT IS NOT A DIR
/var/mobile/Containers/Data/Application/E3...94/Documents/Projects - IT IS NOT A DIR
它就像神奇地转换成一个文件...问题是,当我想查看目录的内容时,我收到一条错误消息,提示它不是一个目录。
我做错了什么吗?这个问题快把我逼疯了...
提前感谢大家的帮助。
编辑:
在第一次运行时,创建目录后,我下载一些文件并使用 dropbox API 将它们保存到新创建的目录中:[self.restClient loadFile:plan.path intoPath:self.projectsLocalPath];
。将文件保存到该目录没有错误。
最佳答案
通过调用:
[self.restClient loadFile:plan.path intoPath:self.projectsLocalPath];
您要求其余客户端将文件放在文件夹位置 - 替换文件夹 - 而不是将其放在文件夹中。
您需要创建目标文件的完整路径,包括文件夹内的文件名。
关于ios - 目录神奇地变成文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30328081/