Why enums are powerful in Swift
📖 Scenario: Imagine you are building a simple app that tracks the status of tasks. Each task can be in one of several states like not started, in progress, or completed. Using enums in Swift helps you manage these states clearly and safely.
🎯 Goal: You will create an enum to represent task states, add a variable to hold the current state, write a function to describe the state, and finally print the description. This shows how enums make your code easier to read and less error-prone.
📋 What You'll Learn
Create an enum called
TaskState with cases notStarted, inProgress, and completedCreate a variable called
currentState of type TaskState and set it to .notStartedWrite a function called
describeState that takes a TaskState parameter and returns a String descriptionUse a
switch statement inside describeState to return different messages for each casePrint the description of
currentState by calling describeState💡 Why This Matters
🌍 Real World
Enums are used in apps to represent fixed sets of options like states, modes, or categories. This helps keep code organized and reduces bugs.
💼 Career
Understanding enums is important for Swift developers because they are widely used in iOS and macOS app development to write clean, safe, and maintainable code.
Progress0 / 4 steps