find the name of a directory in swift

To find the name of a directory in Swift, you can use the URL and FileManager classes. Here's an example:

main.swift
// specify the directory path
let directoryPath = "/path/to/directory"

// create a URL object using the directory path
let directoryURL = URL(fileURLWithPath: directoryPath)

// get the name of the directory from the URL
let directoryName = directoryURL.lastPathComponent

print(directoryName) // prints the name of the directory
331 chars
11 lines

In this code, we first specify the path to the directory. Then we create a URL object from the path using the fileURLWithPath method. Finally, we use the lastPathComponent property of the URL object to get the name of the directory.

Note that the FileManager class provides additional methods for working with directories and files in the file system.

gistlibby LogSnag