Tsich'i
Tsich'i

Reputation: 139

what is the meaning of fileExistsAtPath:isDirectory:'s parameters?

I am a freshman , and i have a problem on the of my study.

When i use the fileExistsAtPath:isDirectory: method ,i don't know what the Parameters's meaning after isDirectory.

I saw that the Parameters after isDirectory is always NO in many code, when they want to confirm the existence of a folder.The documentation said that "contains YES if path is a directory or if the final path element is a symbolic link that points to a directory".i think it should be set to YES.

This is my code:

NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if ([fileManager fileExistsAtPath:@"myDocPath" isDirectory:&isDir] == YES)
{
    NSLog(@"Directory is Exists!");
}
    else
    {
        NSLog(@"Directory is not Exists!");
    }

Thanks for your help , and my english is very bad :)

Upvotes: 0

Views: 263

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89559

Welcome to Stackoverflow! I hope you enjoy participating here.

NSFileManager's fileExistsAtPath: isDirectory: method takes a path (a NSString object) and the "isDirectory:" part is a BOOL address (of a BOOL variable you declared before calling this method), which means the method tells you if the file pointed to in the path is actually a folder (or directory), then it returns YES.

So if you call it via:

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");
}

You should see how the parameter works.

Upvotes: 1

Related Questions