{"id":15921,"date":"2016-10-04T10:08:48","date_gmt":"2016-10-04T14:08:48","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15921"},"modified":"2016-10-04T10:08:48","modified_gmt":"2016-10-04T14:08:48","slug":"option-sets-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/10\/04\/option-sets-in-swift\/","title":{"rendered":"Option Sets in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2016\/09\/swift-option-sets\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2016\/09\/swift-option-sets\/\">\n<p>Swift imports this definition not as an <a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Enumerations.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH12-ID145\">enum<\/a>, but as a <a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/ClassesAndStructures.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH13-ID82\">struct<\/a> that conforms to the <a href=\"https:\/\/developer.apple.com\/reference\/swift\/optionset\"><code>OptionSet<\/code><\/a> protocol. Why a struct and not an enum? Enums are great when the cases are <em>mutually exclusive<\/em>, i.e. only one option can be set at a time. But you can&rsquo;t combine multiple enum cases into a single value in Swift &mdash; unlike in C, where an enum is treated like an integer by the compiler and can assume any value.<\/p>\n<p>An option set struct uses the same efficient representation as a bit field in C, but it presents itself externally <em>as a set<\/em> whose members are the selected options. This allows you to manipulate the bit field with standard <a href=\"https:\/\/en.wikipedia.org\/wiki\/Set_(mathematics)#Basic_operations\">set operations<\/a>, such as testing for membership with <a href=\"https:\/\/developer.apple.com\/reference\/swift\/optionset\/1641006-contains\"><code>contains<\/code><\/a> or combining two bit fields with <a href=\"https:\/\/developer.apple.com\/reference\/swift\/optionset\/1641498-union\"><code>union<\/code><\/a>. And because <code>OptionSet<\/code> inherits from <a href=\"https:\/\/developer.apple.com\/reference\/swift\/expressiblebyarrayliteral\"><code>ExpressibleByArrayLiteral<\/code><\/a>, you can populate an option set with an array literal[&#8230;]<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: Swift imports this definition not as an enum, but as a struct that conforms to the OptionSet protocol. Why a struct and not an enum? Enums are great when the cases are mutually exclusive, i.e. only one option can be set at a time. But you can&rsquo;t combine multiple enum cases into a [&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":[46,71,901],"class_list":["post-15921","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15921","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=15921"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15921\/revisions"}],"predecessor-version":[{"id":15922,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15921\/revisions\/15922"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}