Mar*_*man 15 directory file objective-c 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");
Run Code Online (Sandbox Code Playgroud)
    Mar*_*bri 28
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.");
}
Run Code Online (Sandbox Code Playgroud)
        在 Swift 5 中,您可以使用以下 macOS Playground 示例代码之一检查 URL 路径是否代表目录或常规文件。
URL的hasDirectoryPath属性import Foundation
let url = URL(fileURLWithPath: "/Users/User/Desktop")
print("is directory:", url.hasDirectoryPath)
Run Code Online (Sandbox Code Playgroud)
Filemanager的attributesOfItem(atPath:)方法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)
}
Run Code Online (Sandbox Code Playgroud)
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)
}
Run Code Online (Sandbox Code Playgroud)
URLResourceValuesimport 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")
}
Run Code Online (Sandbox Code Playgroud)
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")
}
Run Code Online (Sandbox Code Playgroud)
FileManager的fileExists(atPath:isDirectory:)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)
Run Code Online (Sandbox Code Playgroud)
        从 [iOS 9、macOS 10.11、tvOS 9.0、watchOS 2.0] 开始,有hasDirectoryPath:
url.hasDirectoryPath
Run Code Online (Sandbox Code Playgroud)