find the path of a directory in swift

To find the path of a directory in Swift, you can use the URL class provided by the Foundation framework. Here's an example code block that demonstrates how to do this:

main.swift
let fileManager = FileManager.default // Get the default file manager instance
let directoryUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! // Get the URL of the documents directory

print(directoryUrl.path) // Print the path of the directory
271 chars
5 lines

In this example, we first get an instance of the default file manager by calling FileManager.default. Then, we use the urls(for:in:) method on the file manager to get an array of URLs for the specified directory and domain mask. In this case, we're getting the URLs for the documents directory in the user's domain. We then take the first URL from the array and force unwrap it using the ! operator to get the URL of the directory.

Finally, we print the path of the directory by accessing the path property of the directoryUrl variable.

gistlibby LogSnag