Our string-based enum seems perfect for our title information, except that we have a case called other. If the person has a title that we hadn't considered when defining the enum, we can choose other, but that doesn't capture what the other title is. In our model, we would need to define another property to hold the value given for other, but that splits our definition of title over two separate properties, which could cause an unintended combination of values.
Swift enums have a solution for this situation, associated values. We can choose to associate a value with each enum case, allowing us to bind a non-optional string to our other case.
Let's rewrite our Title enum to use an associated value:
enum Title {
case mr
case mrs
case mister
case miss
case dr
case prof
case other(String)
}
We have defined the other case to have an associated value by putting the value's type in brackets after the case...