To find the name of a file in Swift, you can use the lastPathComponent
method provided by the URL
class. Here's an example:
main.swift147 chars5 lines
In this example, we first create a string path
that represents the path to our file. We then create a URL
object from this path using the fileURLWithPath
method.
Finally, we use the lastPathComponent
method to extract the last component of the path, which is the name of the file. We store this name in the filename
variable and print it out to verify that we've successfully extracted the name of the file.
gistlibby LogSnag