

{"id":120584,"date":"2024-01-01T18:00:55","date_gmt":"2024-01-01T12:30:55","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=120584"},"modified":"2024-01-01T18:23:34","modified_gmt":"2024-01-01T12:53:34","slug":"swift-enumerations","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/","title":{"rendered":"Swift Enumerations"},"content":{"rendered":"<p>Enumerations are a custom data type. It makes a group of related values as a single data type. It provides type safety for various cases or states. It is also known as enums.<\/p>\n<p>In this article, we\u2019ll learn about its declaration, usage and concepts related to it. We understand this with the help of relevant examples.<\/p>\n<h2>Declaration and Usage<\/h2>\n<p>We define an enum with an enum keyword followed by an enum\u2019s name. We enclose the different cases of the enum within curly braces {}. We define the cases with the case keyword followed by the name of the case. The following syntax and example will explain this better.<\/p>\n<p><strong>The syntax of using enums is<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum enumName{\r\n    case caseName1\r\n    case caseName2\r\n    ...\r\n    case caseNameN\r\n}<\/pre>\n<p><strong>An example of an enum is as follows<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum enumExample{\r\n    case Sunny\r\n    case Windy\r\n    case Rainy\r\n}<\/pre>\n<p><strong>We can access each of the cases in the following way<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enumName.caseName<\/pre>\n<h3>Enums with Switch Statement<\/h3>\n<p>The switch statements involve the usage of cases. Enumerations allow a multi-way selection of cases in a switch statement.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum enumExample{\r\n    case DataFlair\r\n    case Swift\r\n    case SwitchStatements\r\n    case Enumerations\r\n}\r\n\r\nvar example = enumExample.DataFlair\r\nswitch(example){\r\n    case .DataFlair:\r\n        print(\"DataFlair selected\")\r\n    case .Swift:\r\n        print(\"Swift selected\")\r\n    case .SwitchStatements:\r\n        print(\"Switch Statements selected\")\r\n    case .Enumerations:\r\n        print(\"Enumerations selected\")\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>DataFlair selected<\/p>\n<h3>Enums with Raw Values<\/h3>\n<p>We can assign constant values to each of the cases in an enum. These values are called Raw values. These values can be of any data type.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum enumExample: Int{\r\n    case thisYear = 2023\r\n    case lastYear = 2022\r\n    case nextYear = 2024\r\n}\r\n\r\nvar year = enumExample.thisYear\r\n\r\nprint(year.rawValue)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>2023<\/p>\n<h3>Enums with Associated Values<\/h3>\n<p>We can have values associated with each case of enums. <strong>For example,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum enumExample{\r\n    case name(String)\r\n    case date(Int, Int)\r\n}\r\n\r\nvar name = enumExample.name(\"DataFlair\")\r\nvar date = enumExample.date(11, 2023)\r\n\r\nprint(name, date)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>name(&#8220;DataFlair&#8221;) date(11, 2023)<\/p>\n<p>A practical example of using enums with associated value is maintaining an inventory. These inventories identify every product with a unique ID. These IDs can be presented using QR codes or UPC codes. The QR code usually depicts a unique string. The UPC code represents a tuple of integer values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum productID{\r\n    case productUPCCode(Int, Int, Int, Int)\r\n    case productQRCode(String)\r\n}\r\n\r\nvar productWithQR = productID.productQRCode(\"SCANTHEQRCODE\")\r\nvar productWithUPC = productID.productUPCCode(6, 10762, 56932, 7)\r\n\r\nprint(productWithQR)\r\nprint(productWithUPC)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>productQRCode(&#8220;SCANTHEQRCODE&#8221;)<br \/>\nproductUPCCode(6, 10762, 56932, 7)<\/p>\n<h3>Distinction between Raw and Associated Values<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Raw Value<\/b><\/td>\n<td><b>Associated Value<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Constant values assigned to cases<\/span><\/td>\n<td><span style=\"font-weight: 400\">Values are assigned based on variable-defined<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">All the raw values must be of the same type<\/span><\/td>\n<td><span style=\"font-weight: 400\">The associated values can be of different types<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Value remains the same throughput<\/span><\/td>\n<td><span style=\"font-weight: 400\">Value varies every time defined<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Eg: <\/span><b>enum{2022, 2023}<\/b><\/td>\n<td><span style=\"font-weight: 400\">Eg: <\/span><b>enum{\u201cDataFlair\u201d, 2023}<\/b><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Conclusion<\/h3>\n<p>Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it. We have seen how to use enums in switch statements. We also learned about raw values and associated values in enums.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enumerations are a custom data type. It makes a group of related values as a single data type. It provides type safety for various cases or states. It is also known as enums. In&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":120586,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27789],"tags":[28460,28459,28287],"class_list":["post-120584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift-tutorials","tag-enumerations-in-swift","tag-swift-enumerations","tag-swift-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift Enumerations - DataFlair<\/title>\n<meta name=\"description\" content=\"Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/swift-enumerations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Enumerations - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/swift-enumerations\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-01T12:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-01T12:53:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift Enumerations - DataFlair","description":"Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/","og_locale":"en_US","og_type":"article","og_title":"Swift Enumerations - DataFlair","og_description":"Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it.","og_url":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-01-01T12:30:55+00:00","article_modified_time":"2024-01-01T12:53:34+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Swift Enumerations","datePublished":"2024-01-01T12:30:55+00:00","dateModified":"2024-01-01T12:53:34+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/"},"wordCount":365,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp","keywords":["enumerations in swift","swift enumerations","swift tutorials"],"articleSection":["Swift Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/swift-enumerations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/","url":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/","name":"Swift Enumerations - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp","datePublished":"2024-01-01T12:30:55+00:00","dateModified":"2024-01-01T12:53:34+00:00","description":"Enumerations make a group of related values as a single data type. This article covers enums and concepts related to it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/swift-enumerations\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/09\/swift-enumerations.webp","width":1200,"height":628,"caption":"swift enumerations"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/swift-enumerations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Swift Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/swift-tutorials\/"},{"@type":"ListItem","position":3,"name":"Swift Enumerations"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=120584"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120584\/revisions"}],"predecessor-version":[{"id":121359,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120584\/revisions\/121359"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/120586"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=120584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=120584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=120584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}