One of the things that puzzled me about Swift when it came out was that you could put functions in an enum! That seemed like a pretty crazy pattern. I’m used to thinking of enums exactly as that – enums where I can specify values that are mapped into number. That’s it!

However, as I’ve been working on a few simple Swift TableViews and CollectionViews this week, I fell in love with how simple the complex Swift enum can make things. I’d like to share the pattern I’ve been experimenting with, and I’d love to hear what you think about it in the comments.

I’m going to build a very simple table view of Minions (from Despicable Me). Each cell in the table view will only have the image and name of the relevant minion:

Besides just mapping a minion to an int, there is a static dictionary that maps the minion’s index to a minion’s name, a function that gets the minion’s name from that static dictionary, and a function that returns the minion’s image.

Notice how the data – index, name, image of the minion – stays together in the enum. In Objective-C, I would have had an enum, an array of minion names, and likely a function to convert minion names into an image, or worse – I could have used Switch statements to make a much longer tableView:cellForRowAtIndexPath: method. In fact, my coworker @louielouie actually calls this the “anti-switch-statement pattern”.

Overall, I’ve been enjoying using this pattern for my new iOS 8 Swift TableViews.

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.

frytaz1

Great idea

dr.obvous

I am sorry but in Objective-C,
This thing would be written as plist in file not as a enum.
and used KVC to map to TableView.

Problem is that most current obj-c programmers
are really c++ and they bring that kind of experience
to iOS and will transfer to Swift.

Karl

A really practical solution! I’ll test this on my own. Thank you for the great idea!

Mike

Thanks for the article. Is there a way to get the number of minions dynamically instead of hard-coding the number? So if you add another minion, you don’t have to also remember to update the count that gets returned from numberOfRowsInSection?

It feels a bit forced. Raw values should be used for compatibility or when they really represent the enum value, and since a minion isn’t (imho) really represented by its index (they could change index or they could even have gaps between their indices) a different approach should be preferred.

Ways that come to mind are a “minionForTableIndex(index: Int) -> Minion” static method of the enum or a “allMinions() -> [Minion]”, still static, and convert to an index.

Glenn Sugden

Found this DuckDuckGo’ing for something else about enums, but thought this was a good exercise. 🙂