TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift enum Examples: case, rawValue

Use enums with cases and access rawValue. Test an enum in an if-statement and a switch.
Enum. A Size is small, medium or large. Often values come in related groups. With an enum, we create a safe enumeration of a group of values.
Advanced features. With enums, we can store unions of values. In a switch, we can use short syntax to match enums. Our enums can have Int, String values.
An example. This program specifies a Size enum, with the cases Small, Medium and Large. We use the "enum" and case keywords to define an enum.

Let: We create a constant enum value that equals Size.Medium. We then test this against the ".Medium" value.

Let

Tip: With enums, we can omit the enum name, and just test against ".Medium," after the type variable type is known.

Result: The enum value equals ".Medium" so true is printed to the console with print().

Swift program that uses enum enum Size { case Small case Medium case Large } // Create a value of Size enum type. let value = Size.Medium // Test the value. if value == .Medium { print(true) } Output true
RawValue. We can specify a type for an enum—this is the raw value. Here we specify an enum that has Int values. We then access the rawValue from an enum value.

Tip: The rawValue returns a value of the type specified in the enum. So here we get the Ints 0 and 1 from rawValue.

Swift program that uses rawValue with enum // Specify a raw value type of Int on the enum. enum Code: Int { case Ruby, Python } // Access a case from the enum. let c = Code.Ruby // ... Access the rawValue from the enum case. let raw = c.rawValue print(raw) // ... This case has a raw value 1 greater. let raw2 = Code.Python.rawValue print(raw2) Output 0 1
Init, rawValue. An enum variable can be created from a rawValue. Here we specify the rawValue of 4 (which matches Fish). This returns an Optional enum.Optional Binding

Here: In the "if let" statement, we assign a variable to the optional's value. We then test the enum type.

Swift program that creates enum from rawValue enum Animal: Int { case Bird = 3 case Fish = 4 case Insect = 5 } // Create Animal enum from a rawValue Int of 4. if let temp = Animal(rawValue: 4) { if temp == Animal.Fish { // The rawValue is equal to Fish. print(true) print(temp.rawValue) } } Output true 4
Switch. This program uses an enum in a switch. It specifies three cases in the enum Importance (on one line). The "Importance" part can be omitted in the switch cases.Switch
Swift program that uses enum with switch enum Importance { case Low, Medium, High } // Assign to an enum value. let value = Importance.High // Switch on the enum value. switch value { case .Low: print("Not important") case .Medium: print("Average") case .High: print("Do it now!") default: break } Output Do it now!
Default warning. In a switch, the Swift compiler will warn if a case is unreachable. It is safe to remove the "default" case if it will never be reached.
Enum, switch warning: main.swift:16:1: Default will never be executed
Argument. An enum can be passed as an argument to a func. We use the enum type in the argument list. An enum in this respect is just like an Int, String or class type.Func
Swift program that uses enum argument, func enum Importance { case Low, Medium, High } func process(i: Importance) { // Handle the enum argument in a func. if i == .Low || i == .Medium { print("Delay") } else { print("Immediate") } } // Call func with enum arguments. process(i: Importance.Low) process(i: Importance.Medium) process(i: Importance.High) Output Delay Delay Immediate
ErrorType. In Swift, errors are specified as enums of type ErrorType. All exceptions thrown must be of this enum type. Enums have a core use in error handling in the language.Try: ErrorType
A review. With enums, we encode "magic constants" or groups of possible values into a single unit. This can be passed to methods. Enums help enforce program correctness.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf