如何确定NSURL是否是目录

时间:2014-03-09 00:57:38

标签: 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");

5 个答案:

答案 0 :(得分:26)

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

答案 1 :(得分:3)

启动[iOS 9,macOS 10.11,tvOS 9.0,watchOS 2.0],有hasDirectoryPath

url.hasDirectoryPath

答案 2 :(得分:3)

使用Swift 5,您可以使用以下macOS Playground示例代码之一检查URL路径是表示目录还是常规文件。


#1。使用URL的{​​{3}}属性

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
print("is directory:", url.hasDirectoryPath)

#2。使用Filemanager的{​​{3}}方法

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop/File.pdf")
let attributes = try! FileManager.default.attributesOfItem(atPath: url.path)
if let type = attributes[FileAttributeKey.type] as? FileAttributeType {
    print("is file:", type == FileAttributeType.typeRegular)
}
import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
let attributes = try! FileManager.default.attributesOfItem(atPath: url.path)
if let type = attributes[FileAttributeKey.type] as? FileAttributeType {
    print("is directory:", type == FileAttributeType.typeDirectory)
}

#3。使用hasDirectoryPath

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
if let resources = try? url.resourceValues(forKeys: [.isDirectoryKey]) {
    let isDirectory = resources.isDirectory ?? false
    print(isDirectory)
} else {
    print("No such file or directory")
}
import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop/File.pdf")
if let resources = try? url.resourceValues(forKeys: [.isRegularFileKey]) {
    let isFile = resources.isRegularFile ?? false
    print(isFile)
} else {
    print("No such file or directory")
}

#4。使用FileManager的{​​{3}}

import Foundation

let url = URL(fileURLWithPath: "/Users/User/Desktop")
var isDirectory: ObjCBool = false
let fileExists = FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory)
print("is directory:", fileExists && isDirectory.boolValue)

答案 3 :(得分:2)

如果您知道文件URL已经标准化,那么您可以测试尾部斜杠。

-URLByStandardizingPath将标准化文件URL,包括如果路径是目录,则确保尾部斜杠。

这是一个测试,显示-URLByStandardizingPath添加尾部斜杠:

// Get a directory, any directory will do
NSURL *initialURL = [[NSBundle mainBundle] bundleURL];
NSString *initialString = [initialURL absoluteString];

// String the trailing slash off the directory
NSString *directoryString = [initialString substringToIndex:[initialString length] - 1];
NSURL *directoryURL = [NSURL URLWithString:directoryString];

XCTAssertFalse([[directoryURL absoluteString] hasSuffix:@"/"],
               @"directoryURL should not end with a slash");

XCTAssertTrue([[[directoryURL URLByStandardizingPath] absoluteString] hasSuffix:@"/"],
              @"[directoryURL URLByStandardizingPath] should end with a slash");

如您所见,[[[directoryURL URLByStandardizingPath] absoluteString] hasSuffix:@"/"]是测试。

答案 4 :(得分:0)

在Swift 3中启动iOS 8,有isDirectory

(try? url.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false