An enumeration defines a common type for a group of related values so that you can work with those values in a type-safe manner within your code.
enum FamilyUnit {
  case father
  case mother
  case child
var theJoes: FamilyUnit
theJoes = .father
theJoes = .mother
theJoes = .child

if theJoes == .child {
  print("Hello dear child!")

func isPadreDeFamilia(_ member: FamilyUnit) -> Bool {
  return (member == .father)

isPadreDeFamilia(.father) //-> true


The default format
The single line format

Swift enumerations are first-class types, in the same vein as a class.

The case values defined above, regardless of the format, are called the enumeration cases.

Like a regular type, enum names should start with a capital letter and should be camelcased. But, enum names should be singular (not plural) so that they read well:

enum and the switch statement

Associated Values

Here we're specifying that if the case value is father or mother, we'd like a "name" to go with it; and if the case value is child, we'd like to know the child's "ordinal position" (if there are two or more of them) and the child's "name" :

Although in the example above we use constants (let) for extracting associated values—the ones used inside the case statements—nothing could prevent us from using variables (var) if we choose to.

Nothing could even prevent us from mixing together constants and variables:

Notice that we even changed context for the Int portion of the tuple. Whereas we previously used it to associate the child's ordinal position among his/her siblings, this time we used it to associate the child's age instead. So there really is no restrictions in which context you use the associated value types, just as long as it is the correct type.

Lastly, if all the associated values for our enum case are going to be extracted as constants or variables, we may simply place a let or var keyword before the case name, like so:

This makes for brevity of code.

Raw Values

Unlike associated values where cases can come in different types, raw values must be of the same type all throughout:

Implicit Values

The implicit value for each case depends if the type of values stored is String or Int. In our example above, our enumeration stores String values so the implicit value for each case is the String text of its case name.

It is different for Ints:

The implicit value of each succeeding case is one more than the previous. If the first case—father—doesn't have an int value assigned, its value is set to 0, then mother is set to 1, and so on; or, if it's initially set to 10, then mother is set to 11, and child is set to 12.

Raw Value Initializer

Notice the optional binding? Raw value initializers are what is called a failable initializer, meaning, there is a chance it may not return an enumeration case. This happens when the initializer cannot find a matching raw value. When this happens, instead of throwing an exception, it returns a nil. But, if it finds a match, it returns an optional enumeration case, hence, the need for unwrapping.