Associated Values in Enums in Swift 1.2

Swift’s Associated Values for Enums are Awesome.

When I first started writing Swift full time about 6 months ago, I only had a simple understanding of what they actually were. For example, say we’re building template builder for a UITextView that has differently styled parts (think HTML). I’d start with something like this:

Nothing crazy. We want to be able to easily build a styled body of text. And it works, but it feels a little clunky. What are a couple ways we could clean this up?

  1. Create a protocol that has a property of attributes and make a bunch of classes that implement said protocol.
  2. Use inheritence
  3. Utilize associated values in Enums!

What Exactly is an Associated Value?

Associated values allow our cases to have actual inputs. Instead of the enum case returning a rawValue, we instead can allow for any number of properties to be associated (get it?) with it.

I had a difficult time (more so than I should have) initially figuring out what this actually meant, but I think the example above will prove to be super clear. Let’s change up our Enum to look like this:

What we’re saying is that (for example) in order to specify something as a Header, you must specify a string to go along with it. So what does this look like in application? We can now do the following:

 

What’s even nicer is that we can specify multiple associate values. Want to include an image along with a paragraph? Add it as another value. Maybe you’d like the ability to override a default styling? Have it included as an optional!

One final thing that I really like is that it really allows you to write some pretty easy to read code:

This is only scratching the surface of what they can do. We can get super tricky with implementing various protocols on the Enums. Swift 2 will also be adding in the ability to do where clause matching on the enum case’s and their associated values, so we can get even more explicit in how handle different situations.