{"id":35668,"date":"2022-04-25T17:27:47","date_gmt":"2022-04-25T21:27:47","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35668"},"modified":"2022-04-25T17:27:47","modified_gmt":"2022-04-25T21:27:47","slug":"heterogeneous-swift-dictionary-with-strong-types","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/04\/25\/heterogeneous-swift-dictionary-with-strong-types\/","title":{"rendered":"Heterogeneous Swift Dictionary With Strong Types"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/2022\/heterogeneous-dictionary\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/2022\/heterogeneous-dictionary\/\">\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/environment\">environment in SwiftUI<\/a> is sort of like a global dictionary but with stronger types: each key (represented by a key path) can have its own specific value type. For example, the <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/environmentvalues\/isenabled\"><code>\\.isEnabled<\/code><\/a> key stores a boolean value, whereas the <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/environmentvalues\/font\"><code>\\.font<\/code><\/a> key stores an <code>Optional&lt;Font&gt;<\/code>.<\/p>\n\n<p>I wrote a custom dictionary type that can do the same thing. The <code>HeterogeneousDictionary<\/code> struct I show in this article stores mixed key-value pairs where each key defines the type of value it stores. The public API is fully type-safe, no casting required.<\/p>\n<\/blockquote>\n\n<p>This is also similar to what the new <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/attributedstring\">AttributedString<\/a> type uses. <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/urlresourcevalues\">URLResourceValues<\/a> takes a different approach, using a <code>struct<\/code> with optional properties.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: The environment in SwiftUI is sort of like a global dictionary but with stronger types: each key (represented by a key path) can have its own specific value type. For example, the \\.isEnabled key stores a boolean value, whereas the \\.font key stores an Optional&lt;Font&gt;. I wrote a custom dictionary type that can [&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-04-25T21:27:59Z","apple_news_api_id":"6d1e8e77-41ea-460e-b182-285ce2d5254c","apple_news_api_modified_at":"2022-04-25T21:27:59Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AbR6Od0HqRg6xgihc4tUlTA","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,2078,30,2077,71,901,1812],"class_list":["post-35668","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35668","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=35668"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35668\/revisions"}],"predecessor-version":[{"id":35669,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35668\/revisions\/35669"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}