objective-c - 如何确定 NSURL 是否是一个目录

标签 objective-c file directory nsurl nsfilemanager

我有一个 NSURL 对象。它具有文件系统元素的地址,它可以是文件或目录。我希望能够判断 NSURL 是目录还是文件。

这个我已经试过了,好像不行!

NSURL * temp ....... ;// it is initialized and has a valid value 
CFURLRef xx = (CFURLRef)CFBridgingRetain(temp);
if(CFURLHasDirectoryPath(xx)) NSLog(@"was a file");
else NSLog(@"was a folder");

最佳答案

NSNumber *isDirectory;

// this method allows us to get more information about an URL. 
// We're passing NSURLIsDirectoryKey as key because that's the info we want to know.
// Also, we pass a reference to isDirectory variable, so it can be modified to have the return value
BOOL success = [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];

// If we could read the information and it's indeed a directory
if (success && [isDirectory boolValue]) {
    NSLog(@"Congratulations, it's a directory!");
} else {
    NSLog(@"It seems it's just a file.");
}

关于objective-c - 如何确定 NSURL 是否是一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277117/

相关文章:

javascript - 在 UIWebView 上使用 Javascript 获取特定 div 类的所有内容

Java 列出目录中的文件

ios - 你怎么称呼这个在 iOS 中查看的堆叠文件夹?

python - 为什么从文件数据打印时出现错误,但从命令行打印时却没有

powershell - 无法远程创建文件夹

ios - 无法识别的选择器发送到数组分配的实例

ios - UIScrollView 不能正确滚动。

iphone - 如何获取我在iphone中向谁发送电子邮件的数据

java - 创建文件夹

c - 将结构体写入文件中,C