How to enumerate an enum with String type?

Cover Image for How to enumerate an enum with String type?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🃏 How to Enumerate an Enum with String Type in Swift? 🎴

Do you ever find yourself with an enum that has string values and wonder how to iterate over each case? 🤔 Fear not, because in this guide, we will show you how to enumerate an enum with a string type in Swift and provide you with an easy solution! 💪

The Enum Dilemma 😱

Let's say we have the following enum called Suit with string values representing different card suits: ♠️, ♥️, ♦️, and ♣️.

enum Suit: String {
    case spades = "♠️"
    case hearts = "♥️"
    case diamonds = "♦️"
    case clubs = "♣️"
}

Now, imagine you want to perform some operation or print all the raw values of each suit in a loop. You might initially think that using a for-in loop would do the trick, just like in the following code snippet:

for suit in Suit {
    // do something with suit
    print(suit.rawValue)
}

But, wait! When you try to compile this, you'll encounter a compiler error that says, "Enum case 'Suit' not found in type 'Suit.Type'". Oh no, what went wrong? 😱

The Solution 🎉

The error occurs because Suit itself is the type, and you cannot iterate over the type itself. 😕 However, we can easily solve this problem by using the allCases property that is automatically generated for enums that conform to the CaseIterable protocol. 🙌

To make Suit conform to CaseIterable, we just need to add CaseIterable to the enum declaration, like this:

enum Suit: String, CaseIterable { // 🎉 Added CaseIterable here! 🎉
    case spades = "♠️"
    case hearts = "♥️"
    case diamonds = "♦️"
    case clubs = "♣️"
}

Now that Suit conforms to CaseIterable, we can use the allCases property to iterate over each case of the Suit enum. 🎈

Here's the updated code snippet that does exactly that:

for suit in Suit.allCases { // 🎉 Using allCases property here! 🎉
    // do something with suit
    print(suit.rawValue)
}

And voilà! 🎩 You will now see the expected result in your console:

♠️
♥️
♦️
♣️

Wrapping Up 🎁

Enumerating through an enum with string type in Swift is a common problem, but with the CaseIterable protocol and the allCases property, it becomes an easy task. 🚀

Now that you know this handy solution, you can confidently iterate over string-based enums like a pro! 😎

But wait, let's take a moment to connect! 😊 Share your experience with enumerating string enums in the comments below. Have you encountered any challenges, or do you have alternative approaches to share? Let's learn from each other! 💬👇

Happy coding! 💻💙


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello