To find the parent directory of a file in Swift, we can use the URL
and FileManager
classes provided by the Foundation framework. Here's a sample code snippet:
main.swift166 chars5 lines
In this code, we first create a URL
object representing the file's path. Then, we use the deletingLastPathComponent()
method to get a new URL
object representing the parent directory. Finally, we print the path of the parent directory using the path
property.
Note that this code assumes that the file path is valid and that the file exists. You may want to add error handling code to handle cases where these assumptions don't hold.
gistlibby LogSnag