{"id":12600,"date":"2015-10-19T23:47:11","date_gmt":"2015-10-20T03:47:11","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12600"},"modified":"2015-10-19T23:47:11","modified_gmt":"2015-10-20T03:47:11","slug":"options-dictionaries-vs-sets-of-enums","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/10\/19\/options-dictionaries-vs-sets-of-enums\/","title":{"rendered":"Options Dictionaries vs. Sets of Enums"},"content":{"rendered":"<p><a href=\"http:\/\/ericasadun.com\/2015\/10\/19\/sets-vs-dictionaries-smackdown-in-swiftlang\/\">Erica Sadun<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ericasadun.com\/2015\/10\/19\/sets-vs-dictionaries-smackdown-in-swiftlang\/\">\n<p>In Swift, you can re-architect this key-based system into a simple enumeration, specifying the valid type associated with each key case. Here&rsquo;s what this looks like for the five cases listed above. The associated types are pulled from the existing pixel buffer attribute key docs.<\/p>\n<pre>enum CVPixelBufferOptions {\n case CGImageCompatibility(Bool)\n case CGBitmapContextCompatibility(Bool)\n case ExtendedPixelsRight(Int)\n case ExtendedPixelsBottom(Int)\n case PixelFormatTypes([PixelFormatType])\n \/\/ ... etc ...\n}<\/pre>\n<p>Re-designing options this way produces an extensible enumeration with strict value typing for each possible case. This approach offers you a major type safety victory compared to weak dictionary typing.<\/p>\n<\/blockquote>\n<p>However, a limitation is that currently you have to use an array of options rather than a set (unless you want to make the options hashable yourself).<\/p>","protected":false},"excerpt":{"rendered":"<p>Erica Sadun: In Swift, you can re-architect this key-based system into a simple enumeration, specifying the valid type associated with each key case. Here&rsquo;s what this looks like for the five cases listed above. The associated types are pulled from the existing pixel buffer attribute key docs. enum CVPixelBufferOptions { case CGImageCompatibility(Bool) case CGBitmapContextCompatibility(Bool) case [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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":[27,71,901],"class_list":["post-12600","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-craft","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12600","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=12600"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12600\/revisions"}],"predecessor-version":[{"id":12601,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12600\/revisions\/12601"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}