[null, {"document_url": "https://www.dzombak.com/blog/2016/03/Modeling-polymorphic-relationships-in-Swift--spoiler--enums-seem-pretty-cool-.html", "unformatted_src": "/img/blog/2016/03-swift-modeling/common-base-class.png", "src": "https://www.dzombak.com/img/blog/2016/03-swift-modeling/common-base-class.png", "formatted_filename": "common base class", "alt_text": "Imagining this hierarchy with a common base class."}, null, {"document_url": "https://www.dzombak.com/blog/2016/03/Modeling-polymorphic-relationships-in-Swift--spoiler--enums-seem-pretty-cool-.html", "unformatted_src": "/img/blog/2016/03-swift-modeling/common-base-class-with-visual.png", "src": "https://www.dzombak.com/img/blog/2016/03-swift-modeling/common-base-class-with-visual.png", "formatted_filename": "common base class with visual", "alt_text": "A common abstract base class and another abstract Visual Item class."}, null, {"document_url": "https://www.dzombak.com/blog/2016/03/Modeling-polymorphic-relationships-in-Swift--spoiler--enums-seem-pretty-cool-.html", "unformatted_src": "/img/blog/2016/03-swift-modeling/protocols.png", "src": "https://www.dzombak.com/img/blog/2016/03-swift-modeling/protocols.png", "formatted_filename": "protocols", "alt_text": "Using protocols instead of a common abstract base class."}, null]
In our application we have the need to model relationships between—for example—an article and various other media which related to it. Those could be photos, videos, slideshows, or even other articles. In Objective-C, this is commonly achieved in one of two ways.

Aside: I have never properly learned UML, so if you try to interpret these diagrams as strict UML you’ll probably be disappointed.

A common base class One typical approach is to make all these objects inherit from a common base class. (This is a particularly seductive solution when your persistence library requires your models to inherit from a base class, like Core Data or Realm.)

Then, this relationship may look something like:

This solution has a few downsides, though:

And what if we want to restrict a relationship to only certain types of media? For example, perhaps an article can open with a single visual item—an image or video, but not an entire slideshow. The only way to express this limitation in this property’s type is by introducing another abstract class: Clearly, adding a new abstract class to the model layer for each constraint like this is clumsy at best and impossible at worst.


We can solve a few of these problems with Objective-C protocols by using (similar to Java’s interfaces) in our model. Let’s turn Media and VisualItem into protocols: Now we can model shared properties in our Media protocol, we’ve eliminated our abstract classes, and we can trivially add more protocols to express additional constraints if necessary.

But code consuming simply a Media or VisualItem will still need to use the runtime to check what concrete types it’s dealing with—still clumsy.

A Swift solution

In Swift, we’re writing our models as structures (value types, without inheritance) and using protocols to express common properties. We could express these relationships in terms of the Media or VisualItem protocols:

But this approach still suffers from the problems discussed above. Additionally, in Swift:

Luckily, Swift gives us enumerated types, and unlike enums in C they’re very powerful sum types that allow us to attach associated values.

Consider the following enumeration as an addition to the Swift model layer:

This new type now contains the knowledge about valid relationships; rather than making our models conform to a new protocol each time we need to impose an additional constraint, we can easily add a new enum.

(The definition of a RelatedVisualItem enum, encapsulating a relationship only to either a photo or video, is left as an exercise to the reader.)

This lets us rewrite our Article’s relationships as follows:

This approach resolves every problem discussed thus far:

This enum can also be used as a sort of Result return type for a function that may return one of several media types. (As an oversimplified example, imagine a database lookup method, mediaWithID(id: MediaID) -> RelatedMedia.)


In traditional Objective-C code, we’d typically use protocols to express a polymorphic relationship. In Swift, we can take advantage of the language’s powerful enumerations to model these relationships, eliminate runtime type checks, and remove room for error in client code. 💯.


I didn’t come up with this idea! This comes from my coworkers David Galbraith and Steve Matthews.

As always, I welcome discussion and feedback; I’m @cdzombak on Twitter.