{"id":36575,"date":"2022-07-22T17:02:15","date_gmt":"2022-07-22T21:02:15","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36575"},"modified":"2022-07-23T12:45:46","modified_gmt":"2022-07-23T16:45:46","slug":"reverse-engineering-swiftuis-navigationpath-codability","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/07\/22\/reverse-engineering-swiftuis-navigationpath-codability\/","title":{"rendered":"Reverse Engineering SwiftUI&rsquo;s NavigationPath Codability"},"content":{"rendered":"<p><a href=\"https:\/\/www.pointfree.co\/blog\/posts\/78-reverse-engineering-swiftui-s-navigationpath-codability\">Brandon Williams and Stephen Celis<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.pointfree.co\/blog\/posts\/78-reverse-engineering-swiftui-s-navigationpath-codability\">\n<p>An interesting feature of <code>NavigationPath<\/code> is that it is capable of encoding and decoding itself to JSON, even though all of its type information has been erased. This is powerful because it makes state restoration as simple as serializing and deserializing data, but how does it work?<\/p>\n<p>[&#8230;]<\/p>\n<p>Is it possible to recreate this seemingly magical functionality ourselves? Can we really take a nebulous blob of stringy JSON and turn it into values with static types? Well, the answer is yes, by using a little bit of runtime magic and Swift&rsquo;s new existential super powers.<\/p>\n<p>[&#8230;]<\/p>\n<p>We can use an underscored Swift <a href=\"https:\/\/github.com\/apple\/swift\/blob\/c8f4b09809de1fab3301c0cfc483986aa6bdecfa\/stdlib\/public\/core\/Misc.swift#L87-L94\">function<\/a> that is capable of turning a type into a string.<\/p>\n<p>[&#8230;]<\/p>\n<p>Just as there is an underscored Swift function for turning a type into a string, there is also <a href=\"https:\/\/github.com\/apple\/swift\/blob\/c8f4b09809de1fab3301c0cfc483986aa6bdecfa\/stdlib\/public\/core\/Misc.swift#L118-L127\">one<\/a> that goes in the reverse direction[&#8230;]<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/06\/17\/swiftui-changes-at-wwdc-2022\/\">SwiftUI Changes at WWDC 2022<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2022\/06\/17\/swift-5-7-announced\/\">Swift 5.7 Announced<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Brandon Williams and Stephen Celis: An interesting feature of NavigationPath is that it is capable of encoding and decoding itself to JSON, even though all of its type information has been erased. This is powerful because it makes state restoration as simple as serializing and deserializing data, but how does it work? [&#8230;] Is it [&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-07-22T21:02:17Z","apple_news_api_id":"f9a4472e-97f9-46fd-8e94-1e639a7ec569","apple_news_api_modified_at":"2022-07-23T16:45:49Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/A-aRHLpf5Rv2OlB5jmn7FaQ","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,1013,71,1774,901,1812],"class_list":["post-36575","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-16","tag-mac","tag-macos-13-ventura","tag-private-api","tag-programming","tag-swift-codable","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36575","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=36575"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36575\/revisions"}],"predecessor-version":[{"id":36576,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36575\/revisions\/36576"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}