Iterating enum cases with CaseIterable
📖 Scenario: You are creating a simple app that shows different fruit options to users. You want to list all the fruits you support in your app.
🎯 Goal: Build a Swift program that defines an enum of fruits and uses CaseIterable to list all fruit cases.
📋 What You'll Learn
Create an enum called
Fruit with cases apple, banana, and cherryMake the
Fruit enum conform to CaseIterableCreate a variable called
allFruits that holds all cases of FruitUse a
for loop with variable fruit to iterate over allFruitsPrint each fruit case inside the loop
💡 Why This Matters
🌍 Real World
Enums with CaseIterable are useful when you want to list all options of a category, like fruit types, app themes, or user roles.
💼 Career
Understanding how to iterate enum cases helps in building clean, maintainable code for apps that need to handle fixed sets of options.
Progress0 / 4 steps