0

我是大一新生,我的学习有问题。

当我使用fileExistsAtPath:isDirectory: 方法时,我不知道isDirectory 后面的Parameters 是什么意思。

我看到 isDirectory 之后的参数在许多代码中总是 NO,当他们想确认文件夹的存在时。文档说“如果路径是目录或者最终路径元素是指向的符号链接,则包含 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!");
    }

谢谢你的帮助,我的英语很差:)

4

1 回答 1

1

欢迎来到 Stackoverflow!我希望你喜欢在这里参与。

NSFileManager 的fileExistsAtPath: isDirectory:方法接受一个路径(一个 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");
}

您应该看到参数是如何工作的。

于 2013-09-25T03:26:13.123 回答