{"id":36392,"date":"2022-06-30T14:37:33","date_gmt":"2022-06-30T18:37:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36392"},"modified":"2022-06-30T14:37:33","modified_gmt":"2022-06-30T18:37:33","slug":"swift-url-improvements-at-wwdc-2022","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/06\/30\/swift-url-improvements-at-wwdc-2022\/","title":{"rendered":"Swift URL Improvements at WWDC 2022"},"content":{"rendered":"<p><a href=\"https:\/\/nilcoalescing.com\/blog\/GetURLsForSystemFolders\/\">Natalia Panferova<\/a> (<a href=\"https:\/\/twitter.com\/natpanferova\/status\/1542032836990930947\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/GetURLsForSystemFolders\/\">\n<p>Previously, to obtain the path to a file in the application folder, we had to use <code>FileManager<\/code> APIs, which are quite cumbersome. Now, we can get the paths to system folders directly from static properties on <code>URL<\/code> type.<\/p>\n<\/blockquote>\n<p>However, it sounds like the new API&mdash;which does not throw any errors&mdash;does not guarantee that the folder actually exists.<\/p>\n\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/GetURLsForSystemFolders\/\"><p>Another great improvement is the new <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\/3988449-appending\">appending(path:directoryHint:)<\/a> method, that replaces the old <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\/1779992-appendingpathcomponent\">appendingPathComponent(_:isDirectory:)<\/a>. The new method accepts a <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\/directoryhint\">DirectoryHint<\/a> parameter, which indicates whether Foundation should perform a file system check to determine if the path refers to a folder. The default value is <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\/directoryhint\/inferfrompath\">inferFromPath<\/a>, 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.<\/p><\/blockquote>\n<p>That deserves a &ldquo;finally,&rdquo; because for 20+ years most code (which omits the <code>isDirectory<\/code> parameter) has been incurring overhead for accessing the file system every time a URL is created.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2021\/12\/15\/proposed-foundation-url-improvements\/\">Proposed Foundation URL Improvements<\/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<\/ul>","protected":false},"excerpt":{"rendered":"<p>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&mdash;which does not throw any errors&mdash;does not guarantee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-06-30T18:37:35Z","apple_news_api_id":"726143ef-260c-4d92-83e9-c02f302bb087","apple_news_api_modified_at":"2022-06-30T18:37:36Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AcmFD7yYMTZKD6cAvMCuwhw","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":[31,2185,30,2223,138,71,901,489],"class_list":["post-36392","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-16","tag-mac","tag-macos-13-ventura","tag-optimization","tag-programming","tag-swift-programming-language","tag-url"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36392","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=36392"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36392\/revisions"}],"predecessor-version":[{"id":36393,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36392\/revisions\/36393"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}