{"id":34443,"date":"2021-12-15T16:33:00","date_gmt":"2021-12-15T21:33:00","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=34443"},"modified":"2025-10-29T09:39:55","modified_gmt":"2025-10-29T13:39:55","slug":"proposed-foundation-url-improvements","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/12\/15\/proposed-foundation-url-improvements\/","title":{"rendered":"Proposed Foundation URL Improvements"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\">Charles Hu<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\"><p><code>URL<\/code> is one of the most common types used by Foundation for File IO as well as network-related tasks. The goal of this proposal is to improve the ergonomics of <code>URL<\/code> by introducing some convenience methods and renaming some verbose\/confusing ones. Specifically, we proposed to:<\/p><ul><li>Introduce a new <code>StaticString<\/code> initializer<\/li><li>Refine the existing &ldquo;file path&rdquo; initializers<\/li><li>Refine <code>appendingPathComponent<\/code> and friends<\/li><li>Introduce common directories as static <code>URL<\/code> properties<\/li><\/ul><p>[&#8230;]<\/p><p>We propose to add all &ldquo;get URL&rdquo; style methods from <code>FileManager<\/code> to <code>URL<\/code> as static methods. This allows the call site to use Swift&rsquo;s powerful static member lookup to get the URLs to predefined directories instead of always needing to spell out <code>FileManager.default<\/code>. We also propose to add a few more static directory URLs that correspond to <code>FileManager.SearchPathDirectory<\/code>.<\/p><\/blockquote>\n<p>Aside from being verbose, I&rsquo;ve never liked the <code>FileManager.urls(for:in:)<\/code> API because it returns an array that could potentially be empty. So I have to either risk a crash or check for that case and either report an error or substitute my own fallback. The proposed new API simply offers properties (which can&rsquo;t throw) for the basic directories that I care about.<\/p>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/15\">Karl<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/15\"><p>Unfortunately, <code>URL.appendingPathComponent<\/code> just <a href=\"https:\/\/github.com\/apple\/swift-corelibs-foundation\/blob\/73d1943d0184040688a0f3ba5d66b61ea9e10a09\/Sources\/Foundation\/URL.swift#L784\">forwards<\/a> to <code>NSURL.appendingPathComponent<\/code>, which makes <a href=\"https:\/\/github.com\/apple\/swift-corelibs-foundation\/blob\/73d1943d0184040688a0f3ba5d66b61ea9e10a09\/Sources\/Foundation\/NSURL.swift#L860\">blocking calls to the filesystem<\/a>. So does <code>URL(fileURLWithPath:)<\/code> (<a href=\"https:\/\/github.com\/apple\/swift-corelibs-foundation\/blob\/73d1943d0184040688a0f3ba5d66b61ea9e10a09\/Sources\/Foundation\/NSURL.swift#L217\">here<\/a>).<\/p>\n<p>But I do agree that a URL type should be a pure model type, and that the results of URL manipulation should never depend on the state of the filesystem.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/21\">Thomas Clement<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/21\"><p>This has always bothered me as well, because what if the path doesn&rsquo;t actually exist on disk at this point? Or what if the path isn&rsquo;t readable from the current process permissions? Or what if the items on disk are later replaced by others (e.g. file could turn into a directory).<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/18\">Karl<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/18\"><p>It sounds weird - like, surely everybody would have noticed already if URLs <em>didn&rsquo;t<\/em> conform to <code>Comparable<\/code> - but it&rsquo;s true! It doesn&rsquo;t conform!<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/29\">Alejandro Martinez<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/foundation-url-improvements\/54057\/29\">\n<p>It&rsquo;s probably out of scope for this but my biggest issue with URL is its strict parsing. The amount of times a feature breaks because suddenly we get a url with spaces that URL refuses to parse it&rsquo;s not small.<\/p>\n<\/blockquote>\n<p>Everyone who deals with lots of URLs seems to have their own code to try to &ldquo;fix up&rdquo; invalid URLs that they get from user input or other external sources. It probably makes sense for <code>URL<\/code> to be strict by default, but it would be nice to have an option for more leniency, e.g. along the lines of what Safari does if you type in something that doesn&rsquo;t follow RFC 1738.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/01\/11\/swift-filepath-syntactic-operations\/\">Swift FilePath Syntactic Operations<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/03\/29\/the-weirdness-of-nsurls-isdirectory-flag\/\">The Weirdness of NSURL&rsquo;s isDirectory Flag<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2013\/07\/13\/url-path-retrieval-cheat-sheet\/\">URL Path Retrieval Cheat Sheet<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2012\/12\/11\/nsurl-path-handling\/\">NSURL Path Handling<\/a><\/li>\n<\/ul>\n\n<p id=\"proposed-foundation-url-improvements-update-2025-07-22\">Update (<a href=\"#proposed-foundation-url-improvements-update-2025-07-22\">2025-07-22<\/a>): <a href=\"https:\/\/mastodon.social\/@rosyna\/114897245618518159\">Rosyna Keller<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@rosyna\/114897245618518159\">\n<p>I didn&rsquo;t realize how much slower <code>URL(fileURLWithPath:)<\/code> is vs using <code>URL(filePath:)<\/code> is.<\/p>\n<p>Making <em>only<\/em> that change, timing in my loop went from 4 seconds to 1.4 seconds.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Charles Hu: URL is one of the most common types used by Foundation for File IO as well as network-related tasks. The goal of this proposal is to improve the ergonomics of URL by introducing some convenience methods and renaming some verbose\/confusing ones. Specifically, we proposed to:Introduce a new StaticString initializerRefine the existing &ldquo;file path&rdquo; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-12-15T21:33:02Z","apple_news_api_id":"602a0433-b848-4a49-b9fa-8d235617e7ef","apple_news_api_modified_at":"2025-07-22T19:01:55Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AYCoEM7hISkm5-o0jVhfn7w","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,30,71,2854,901,489],"class_list":["post-34443","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-mac","tag-programming","tag-swift-foundation","tag-swift-programming-language","tag-url"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=34443"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34443\/revisions"}],"predecessor-version":[{"id":48566,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/34443\/revisions\/48566"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=34443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=34443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=34443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}