{"id":9028,"date":"2022-01-04T21:14:28","date_gmt":"2022-01-04T21:14:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9028"},"modified":"2022-01-04T21:14:28","modified_gmt":"2022-01-04T21:14:28","slug":"swift-enumerations","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/swift-enumerations\/","title":{"rendered":"Swift enumerations"},"content":{"rendered":"<p>Swift enumerations are similar like Java enum types in that they can declare functionality as well as the usual values.<\/p>\n<p>To declare a basic enumeration we use the following syntax<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Color {\r\n  case red\r\n  case white\r\n  case blue\r\n  \/\/ ... etc.\r\n}\r\n<\/pre>\n<p>We can also declare using associated values, i.e.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Color: Int {\r\n  case red = 1\r\n  case white\r\n  case blue\r\n  \/\/ ... etc.\r\n}\r\n<\/pre>\n<p>We can also declare enums as strings, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Color: String {\r\n  case red = &quot;Red&quot;\r\n  case white = &quot;White&quot;\r\n  case blue = &quot;Blue&quot;\r\n  \/\/ ... etc.\r\n}\r\n<\/pre>\n<p>Enums can also have methods, so for example (a rather contrived example)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Color {\r\n  case red\r\n  case white\r\n  case blue\r\n  \/\/ ... etc.\r\n\r\n  func isRed() -&gt; Bool {\r\n    self == .red \/\/ .red shows abbreviated syntax\r\n  }    \r\n}\r\n<\/pre>\n<p>We can also declare enum cases with associated data\/properties, so for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Device {\r\n  case phone(model: String)\r\n  case tablet(model: String)\r\n  case desktop(model: String)\r\n}\r\n\r\nlet device = Device.phone(&quot;Samsung&quot;)\r\n\r\nswitch device {\r\n  case .phone(let model)\r\n    print(&quot;Phone: \\(model)&quot;)\r\n  case .tablet(let model)\r\n    print(&quot;Tablet: \\(model)&quot;)\r\n  case .desktop(let model)\r\n    print(&quot;Desktop: \\(model)&quot;)\r\n}\r\n<\/pre>\n<p>Recursive enum, where we might refer to the enum within the enum require the use of the keyword <em>indirectcase<\/em>. for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nenum Thing {\r\n  case value(String)\r\n   indirectcase thing(Thing)\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Swift enumerations are similar like Java enum types in that they can declare functionality as well as the usual values. To declare a basic enumeration we use the following syntax enum Color { case red case white case blue \/\/ &#8230; etc. } We can also declare using associated values, i.e. enum Color: Int { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9028","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9028"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9028\/revisions"}],"predecessor-version":[{"id":9063,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9028\/revisions\/9063"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}