我是新生,我的学习有问题。
当我使用fileExistsAtPath:isDirectory:方法时,我不知道isDirectory之后参数的含义。
我看到isDirectory之后的参数在许多代码中始终为NO,当他们想要确认文件夹的存在时。文档说“如果path是目录或者最终路径元素是符号链接,则包含YES指向目录“。我认为它应该设置为YES。
这是我的代码:
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if ([fileManager fileExistsAtPath:@"myDocPath" isDirectory:&isDir] == YES)
{
NSLog(@"Directory is Exists!");
}
else
{
NSLog(@"Directory is not Exists!");
}
感谢您的帮助,我的英语非常糟糕:)
答案 0 :(得分:1)
欢迎使用Stackoverflow!我希望你喜欢在这里参加。
NSFileManager's fileExistsAtPath: isDirectory:
method采用路径(NSString对象),“isDirectory:
”部分是BOOL 地址 (BOOL变量的你)在调用此方法之前声明),这意味着方法告诉您路径中指向的文件实际上是文件夹(或目录),然后返回YES。
因此,如果您通过以下方式致电:
BOOL directoryBool;
BOOL doesSomethingExistHere = [[NSFileManager defaultManager] fileExistsAtPath: @"/etc" isDirectory: &directoryBool];
if(doesSomethingExistHere)
{
NSLog( @"something exists at the path");
if(directoryBool)
NSLog( @"and it's a directory");
else
NSLog( @"it's a file, not a directory");
} else {
NSLog( @"nothing exists at the path you specified");
}
你应该看看参数是如何工作的。