Peter71 Peter71 - 2 months ago 10
Swift Question

Get fullpath or convert to fullpath

When using

let directoryEnumerator = FileManager().enumerator(at: ...

in Swift 3, I get all files from the folder, e.g.


The results are not including the leading path (here "/Volumes/MacOS"). So I get


How can I get the fullpath (directly from the enumerator) or convert them. I want to use URL functions, not string function manipulating by assumptions.


If "MacOS" is the name of your current startup disk then "/Volumes/MacOS" is a symbolic link to "/", so both "/fasttemp/Fotos/2005/" and "/Volumes/MacOS/fasttemp/Fotos/" are absolute paths to the same file.

In order to get a unique file name representation you can query a URL for its canonical path. Example:

let url = URL(fileURLWithPath: "/Volumes/MacOS/Applications/Utilities/")
if let cp = (try? url.resourceValues(forKeys: [.canonicalPathKey]))?.canonicalPath {
// Output: "/Applications/Utilities"