fileExistsAtPath
方法返回一个已存在的目录的值为NO。 如果我有以下代码:
NSURL *newDirectoryPath = ... // path does not begin with a tilda
// and is created using URLByAppendingPathComponent:isDirectory:
BOOL directoryExists = [self.fileManager fileExistsAtPath:[newDirectoryPath absoluteString]];
if (NO == directoryExists)
{
BOOL ret = [self.fileManager moveItemAtURL:self.currentPresentationDirectoryPath toURL:newDirectoryPath error: &err];
// ret is YES, err is nil
directoryExists = [self.fileManager fileExistsAtPath:[newDirectoryPath absoluteString]];
}
尽管使用
moveItemAtURL
创建目录成功,但fileExistsAtPath
仍然返回NO。虽然文档上说“不建议基于文件系统或特定文件的当前状态来预测行为。这样做可能会在文件系统竞态条件下导致奇怪的行为。”,但我还是想理解这里的问题所在。如果我关闭应用程序并重新启动,则上述代码中第一次检查fileExistsAtPath
仍然返回NO,即使目录在先前执行代码期间已成功创建,并且我可以在组织器中看到该目录,还可以成功从目录内容中读取等等。此外,附言:没有fileExistsAtURL
方法吗?