Checking if a directory exists

Swift:
Checking if a directory exists

How to:

Swift’s Foundation framework provides the FileManager class, which has methods to manage the file system. You can use FileManager to check if a directory exists. Here’s a snippet on how to do this:

import Foundation

let fileManager = FileManager.default
let path = "/path/to/your/directory"

if fileManager.fileExists(atPath: path, isDirectory: nil) {
    print("Directory exists")
} else {
    print("Directory does not exist")
}

However, this checks for both files and directories. If you specifically want to verify a directory exists, you need to pass a pointer to a Boolean value in isDirectory:

import Foundation

let fileManager = FileManager.default
let path = "/path/to/your/directory"
var isDirectory: ObjCBool = false

if fileManager.fileExists(atPath: path, isDirectory: &isDirectory), isDirectory.boolValue {
    print("Directory exists")
} else {
    print("Directory does not exist")
}

Using a Third-Party Library

As of now, checking for the existence of a directory in Swift usually doesn’t necessitate third-party libraries due to the robustness of the FileManager class. However, for more complex file manipulation and checking, libraries like Files by John Sundell provide a more Swift-friendly API.

Here’s how you might use it:

First, add Files to your project via Swift Package Manager.

Then, you can check for a directory’s existence like so:

import Files

do {
    _ = try Folder(path: "/path/to/your/directory")
    print("Directory exists")
} catch {
    print("Directory does not exist")
}

Note: As third-party libraries can change, always refer to the latest documentation for usage and best practices.