To find the path of a file in Swift, you can use the FileManager
class to get the URL of the file and then convert it to a string representation of the file path.
Here's an example code snippet:
main.swift266 chars8 lines
In this example, we first create a fileName
constant that represents the name of the file we want to find the path of.
Next, we use FileManager.default.urls
to get an array of URLs for the specified searchPathDirectory
and domainMask
. In this case, we're getting the directory where the user's documents are stored. We then take the first URL in the array and append the fileName
to it, creating a new URL that points to the file.
Finally, we use the path
property of the URL
to get the string representation of the file path and print it to the console. If the file is not found, we print an error message instead.
gistlibby LogSnag