Thursday, June 30, 2022

Swift URL Improvements at WWDC 2022

Natalia Panferova (tweet):

Previously, to obtain the path to a file in the application folder, we had to use FileManager APIs, which are quite cumbersome. Now, we can get the paths to system folders directly from static properties on URL type.

However, it sounds like the new API—which does not throw any errors—does not guarantee that the folder actually exists.

Another great improvement is the new appending(path:directoryHint:) method, that replaces the old appendingPathComponent(_:isDirectory:). The new method accepts a DirectoryHint parameter, which indicates whether Foundation should perform a file system check to determine if the path refers to a folder. The default value is inferFromPath, meaning that this information will be inferred from the path string we pass to the method. If the string contains a trailing slash, it will be treated as a directory path.

That deserves a “finally,” because for 20+ years most code (which omits the isDirectory parameter) has been incurring overhead for accessing the file system every time a URL is created.


Comments RSS · Twitter

Leave a Comment