{"id":20726,"date":"2018-02-27T15:44:32","date_gmt":"2018-02-27T20:44:32","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20726"},"modified":"2019-01-18T10:29:34","modified_gmt":"2019-01-18T15:29:34","slug":"decoding-dictionaries-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/02\/27\/decoding-dictionaries-in-swift\/","title":{"rendered":"Decoding Dictionaries in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/elegantchaos.com\/2018\/02\/21\/decoding-dictionaries-in-swift.html\">Sam Deane<\/a>:<\/p>\n<blockquote cite=\"http:\/\/elegantchaos.com\/2018\/02\/21\/decoding-dictionaries-in-swift.html\"><p><a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/archives_and_serialization\/encoding_and_decoding_custom_types\">Swift&rsquo;s <code>Codable<\/code> support<\/a> is really great for doing this when you&rsquo;ve got JSON or a Plist, but not all dictionary data ultimately lives in a file.<\/p><p>For example, I was recently doing some stuff with the Disk Arbitration framework. This has a <code>DADiskCopyDescription<\/code> call which gives you back a dictionary of known keys (many of which may be missing). This dictionary only ever exists in memory.<\/p><p>What I really want to do in this situation is extract some of these keys into a structure or object as a way of validating that I have what I need, and discarding anything I don&rsquo;t need.<\/p><p>[&#8230;]<\/p><p>The <code>Codable<\/code> support would be ideal here, except that as it comes out-of-the-box, it appears that I&rsquo;d have to convert the dictionary into JSON first in order to convert it back. This seems&#8230; non optimal&#8230; so I set out to make encoder \/ decoder classes which just work with dictionaries.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Sam Deane: Swift&rsquo;s Codable support is really great for doing this when you&rsquo;ve got JSON or a Plist, but not all dictionary data ultimately lives in a file.For example, I was recently doing some stuff with the Disk Arbitration framework. This has a DADiskCopyDescription call which gives you back a dictionary of known keys (many [&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":"2019-01-18T15:29:36Z","apple_news_api_id":"9056b60e-9010-429d-a43e-40030c8e3223","apple_news_api_modified_at":"2019-01-18T15:29:37Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AkFa2DpAQQp2kPkADDI4yIw","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":[],"tags":[69,31,30,74,71,1774,901],"class_list":["post-20726","post","type-post","status-publish","format-standard","hentry","tag-cocoa","tag-ios","tag-mac","tag-opensource","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20726","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=20726"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20726\/revisions"}],"predecessor-version":[{"id":20727,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20726\/revisions\/20727"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}