Getting a File Path From a Swift URL
Seth Willits (2024, bug):
There’s a deprecation warning in Xcode for
URL.paththat reads:WARNING:
pathwill be deprecated in a future version of macOS: Usepath(percentEncoded:)insteadThe problem is something like this:
Bundle.main.url(forAuxiliaryExecutable: "tool")!.path… which returns the expected file path (ex:
/Applications/MyApp.app/Contents/tool)would be quickly rewritten as suggested to:
Bundle.main.url(forAuxiliaryExecutable: "tool")!.path(percentEncoded: false)… which simply returns tool, because the URL returned is relative to a base URL.
[…]
I’ve always found it not-completely-obvious to newcomers how to get a standard file path string from a file URL and this only makes it more confusing.
There are at least three issues here:
url.pathreturns an absolute path, buturl.path(percentEncoded: false)returned a relative path as of his writing. I tested this on macOS 26, and now it returns an absolute path. The documentation now says, “This function will resolve against the baseURL.” It seems the behavior of the method changed without it being documented when, so be careful if you’re deploying to a mix of OS versions.percentEncodedhas a default value oftrue, whereaspathdid not return an encoded string. In other words,url.path()andurl.pathare completely different. So this is another case where Apple updated the API but flipped the default—a good way to introduce bugs.It’s not obvious how to get the full file system path out of a
URL. WithNSURL,url.pathwas natural and did the right thing. Now, theURLequivalent is deprecated.The natural replacement,
url.path(), is wrong. So is Xcode’s recommendation ofurl.(percentEncoded: false).url.absoluteURL.path(percentEncoded: false)is correct but verbose.url.absoluteURL.standardizedFileURL.path(percentEncoded: false)is possibly better but even more verbose.FilePath(url)?.stringuses a newer API but gives you an optional, even though for a file URL it should never fail.This seems like such basic functionality, but there’s no API that’s the obvious right choice, concise, and ergonomic.
Previously:
- The Switch From File Paths to URLs
- Swift URL Improvements at WWDC 2022
- Proposed Foundation URL Improvements
- URL Path Retrieval Cheat Sheet
- NSURL Path Handling