YouTip LogoYouTip

Swift Enumerations

Simply put, an enumeration is also a data type, except that this data type only contains custom specific data. It is a collection of data with common characteristics.\\n\\nSwift enumerations are similar to the structures in Objective C and C. The functionalities of enumerations are:\\n\\n* Declared in a class, its values can be accessed by instantiating the class.\\n\\n* Enumerations can also define constructors (initializers) to provide an initial member value; their functionality can be extended based on the original implementation.\\n\\n* They can conform to protocols to provide standard functionality.\\n\\n### Syntax\\n\\nIn Swift, the `enum` keyword is used to create an enumeration and its entire definition is placed within a pair of curly braces:\\n\\nenum enumname { // Enum definition goes here}\\nFor example, we define the following enumeration representing the days of the week:\\n\\nimport Cocoa// Define enum DaysofaWeek { case Sunday case Monday case TUESDAY case WEDNESDAY case THURSDAY case FRIDAY case Saturday}var weekDay = DaysofaWeek.THURSDAY weekDay = .THURSDAY switch weekDay {case .Sunday: print("Sunday")case .Monday: print("Monday")case .TUESDAY: print("Tuesday")case .WEDNESDAY: print("Wednesday")case .THURSDAY: print("Thursday")case .FRIDAY: print("Friday")case .Saturday: print("Saturday")}\\nThe output of the above program execution is:\\n\\nThursday\\nThe values defined in an enumeration (such as `Sunday`, `Monday`, `……` and `Saturday`) are the **member values** (or **members**) of this enumeration. The `case` keyword indicates that a new member value will be defined.\\n\\n> Note: Unlike C and Objective-C, Swift enumeration members are not assigned a default integer value when created. In the `DaysofaWeek` example above, `Sunday`, `Monday`, `……` and `Saturday` will not be implicitly assigned values of `0`, `1`, `……` and `6`. Instead, these enumeration members themselves have complete values, which are clearly defined `DaysofaWeek` types.\\n\\nvar weekDay = DaysofaWeek.THURSDAY \\nThe type of `weekDay` can be inferred when it is initialized with one of the possible values of `DaysofaWeek`. Once `weekDay` is declared as a `DaysofaWeek`, you can use a shorthand syntax (.) to set it to another `DaysofaWeek` value:\\n\\nvar weekDay = .THURSDAY \\nWhen the type of `weekDay` is known, the enumeration name can be omitted when assigning a value to it again. Using explicitly typed enumeration values makes the code more readable.\\n\\nEnumerations can be divided into associated values and raw values.\\n\\n### Differences between Associated Values and Raw Values\\n\\n| Associated Values | Raw Values |\\n| --- | --- |\\n| Different data types | Same data type |\\n| Instance: enum {10,0.8,"Hello"} | Instance: enum {10,35,50} |\\n| Values are created based on constants or variables | Pre-populated values |\\n| Associated values are set when you create a new constant or variable based on an enumeration member, and each time you do so, its value can be different. | Raw values are always the same |\\n\\n* * *\\n\\n## Associated Values\\n\\nIn the following example, we define an enumeration type named Student, which can be a String for Name, or an associated value (Int, Int, Int) for Mark.\\n\\nimport Cocoaenum Student{ case Name(String) case Mark(Int,Int,Int)}var studDetails = Student.Name("Tutorial")var studMarks = Student.Mark(98,97,95)switch studMarks {case .Name(let studName): print("The student's name is: \\\\(studName)。")case .Mark(let Mark1, let Mark2, let Mark3): print("The student's grade is: \\\\(Mark1),\\\\(Mark2),\\\\(Mark3)。")}\\nThe output of the above program execution is:\\n\\nThe student's grade is: 98,97,95。\\n\\n* * *\\n\\n## Raw Values\\n\\nRaw values can be strings, characters, or any integer or floating-point values. Each raw value must be unique within its enumeration declaration.\\n\\nFor enumerations with integer raw values, you do not need to explicitly assign a value to each member; Swift will automatically assign values for you.\\n\\nFor example, when using integers as raw values, the implicitly assigned values increment by 1. If the first value is not assigned an initial value, it will automatically be set to 0.\\n\\nimport Cocoaenum Month: Int { case January = 1, February, March, April, May, June, July, August, September, October, November, December}let yearMonth = Month.May.rawValue print("The numeric month is: \\\\(yearMonth)。")\\nThe output of the above program execution is:\\n\\nThe numeric month is: 5。
← Swift StructuresSwift Closures β†’