Check if NSURL is a directory

When using Swift, I want to check if the location is a NSURLdirectory. With Objective-C, this is not a problem and a working find, but when I convert the code to Swift, I run a runtime error.

Can someone point me in the right direction?

import Foundation

let defaultManager = NSFileManager.defaultManager()
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
let localDocumentURLs = defaultManager.contentsOfDirectoryAtURL(documentsDirectory,
includingPropertiesForKeys: nil, options: .SkipsPackageDescendants, error: nil) as NSURL[]

for url in localDocumentURLs {
    var isError: NSError? = nil
    var isDirectory: AutoreleasingUnsafePointer<AnyObject?> = nil
    var success: Bool = url.getResourceValue(isDirectory, forKey: NSURLIsDirectoryKey, error: &isError)
}
+8
source share
8 answers

which works well on my side:

var error: NSError?
let documentURL : NSURL = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true, error: &error)
var isDirectory: ObjCBool = ObjCBool(0)
if NSFileManager.defaultManager().fileExistsAtPath(documentURL.path, isDirectory: &isDirectory) {
    println(isDirectory)
}

NOTE: it checks if the folder is a Documentsfolder. of course you can replace the url.

+14
source

In iOS 9. 0+ and macOS 10. 11+ there is a property in NSURL/URL

Swift:

var hasDirectoryPath: Bool { get }

Objective-C:

@property(readonly) BOOL hasDirectoryPath;

URL, API FileManager, , .

URL-, , isDirectory

Swift:

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

Objective-C:

NSNumber *isDirectory = nil;
[url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
NSLog(@"%i", isDirectory.boolValue);
+23

Swift 3

extension URL {
    var isDirectory: Bool {
        let values = try? resourceValues(forKeys: [.isDirectoryKey])
        return values?.isDirectory ?? false
    }
}
+11

2.0 :

        do {
            var rsrc: AnyObject?
            try element.getResourceValue(&rsrc, forKey: NSURLIsDirectoryKey)
            if let isDirectory = rsrc as? NSNumber {
                if isDirectory == true {
                    // ...
                }
            }
        } catch {
        }
+3

2 ... NSURL: ( holex-)

var error: NSError?

// since getting it as URLForDirectory, it flags it as dir, for custom path: NSURL:fileURLWithPath:isDirectory:
let documentURL:NSURL = NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true, error: &error)

// passes true, if it exists and validates as dir (as flagged in the NSURL object)
if (documentURL.checkResourceIsReachableAndReturnError(&error) {
    println(error)
}
+2

... :
FileManager ( NSFileManger).

: FileManager ! FileManager , (, undefined, , ).

FileManager, , , .

, , , :

extension URL {
    var isDirectory: Bool? {
        do {
            let values = try self.resourceValues(
                forKeys:Set([URLResourceKey.isDirectoryKey])
            )
            return values.isDirectory
        } catch  { return nil }
    }
}

What is it. Using the method resourceValues(), you can also easily request other interesting information, such as file size or date of last modification. This is a very powerful API that has existed for centuries NSURLin Obj-C.

+1
source

... and this is an extension of Swift 3 to FileManager:

extension FileManager {
    func isDirectory(url:URL) -> Bool? {
        var isDir: ObjCBool = ObjCBool(false)
        if fileExists(atPath: url.path, isDirectory: &isDir) {
            return isDir.boolValue
        } else {
            return nil
        }
    }
}
0
source

... and quick approach 3 as an extension of the URL:

extension URL {
    func isDirectory() -> Bool? {
        var isDir: ObjCBool = ObjCBool(false)
        if FileManager.default.fileExists(atPath: self.path, isDirectory: &isDir) {
            return isDir.boolValue
        } else {
            return nil
        }
    }
}
-1
source

Source: https://habr.com/ru/post/1544501/


All Articles