Swift Enumerations

This tutorial belongs to the Swift series

Enumerations are a way to group a set of different options, under a common name.

Example:
enum Animal {
case dog
case cat
case mouse
case horse
}
This Animal enum is now a type.

A …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This tutorial belongs to the Swift series

Enumerations are a way to group a set of different options, under a common name.

Example:

enum Animal {
    case dog
    case cat
    case mouse
    case horse
}

This Animal enum is now a type.

A type whose value can only be one of the cases listed.

If you define a variable of type Animal:

var animal: Animal

you can later decide which value to assign it using this syntax:

var animal: Animal
animal = .dog

We can use enumerations in control structures like switches:

enum Animal {
    case dog
    case cat
    case mouse
    case horse
}

let animal = Animal.dog

switch animal {
case .dog: print("dog")
case .cat: print("cat")
default: print("another animal")
}

Enumerations values can be strings, characters or numbers.

You can also define an enum on a single line:

enum Animal {
    case dog, cat, mouse, horse
}

And you can also add type declaration to the enumeration, and each case has a value of that type assigned:

enum Animal: Int {
    case dog = 1
    case cat = 2
    case mouse = 3
    case horse = 4
}

Once you have a variable, you can get this value using its rawValue property:

enum Animal: Int {
    case dog = 1
    case cat = 2
    case mouse = 3
    case horse = 4
}

var animal: Animal
animal = .dog

animal.rawValue //1

Enumerations are a value type. This means they are copied when passed to a function, or when returned from a function. And when we assign a variable pointing to an enumeration to another variable.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-06-08T05:00:00+00:00) Swift Enumerations. Retrieved from https://www.scien.cx/2021/06/08/swift-enumerations/

MLA
" » Swift Enumerations." flaviocopes.com | Sciencx - Tuesday June 8, 2021, https://www.scien.cx/2021/06/08/swift-enumerations/
HARVARD
flaviocopes.com | Sciencx Tuesday June 8, 2021 » Swift Enumerations., viewed ,<https://www.scien.cx/2021/06/08/swift-enumerations/>
VANCOUVER
flaviocopes.com | Sciencx - » Swift Enumerations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/08/swift-enumerations/
CHICAGO
" » Swift Enumerations." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/06/08/swift-enumerations/
IEEE
" » Swift Enumerations." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/06/08/swift-enumerations/. [Accessed: ]
rf:citation
» Swift Enumerations | flaviocopes.com | Sciencx | https://www.scien.cx/2021/06/08/swift-enumerations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.