0
0
Swiftprogramming~15 mins

Enum declaration and cases in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum declaration and cases
📖 Scenario: You are creating a simple app that tracks the weather condition for the day. You want to use an enum to represent different weather types.
🎯 Goal: Build a Swift program that declares an enum called Weather with specific cases, then prints a message based on a selected weather case.
📋 What You'll Learn
Declare an enum named Weather with cases sunny, rainy, and cloudy
Create a variable named todayWeather and assign it the sunny case of Weather
Use a switch statement on todayWeather to print a message for each weather case
Print the message to the console
💡 Why This Matters
🌍 Real World
Enums help represent fixed sets of related values, like weather types, user roles, or app states, making code easier to read and maintain.
💼 Career
Understanding enums is essential for Swift developers building iOS apps, as they are used widely for managing states and options clearly.
Progress0 / 4 steps
1
Declare the enum with weather cases
Declare an enum named Weather with these exact cases: sunny, rainy, and cloudy.
Swift
Need a hint?

Use the enum keyword followed by the name Weather. Inside curly braces, list each case with the case keyword.

2
Create a variable for today's weather
Create a variable named todayWeather and assign it the sunny case of the Weather enum.
Swift
Need a hint?

Use var todayWeather = Weather.sunny to create and assign the variable.

3
Use a switch statement to handle weather cases
Write a switch statement on todayWeather with cases .sunny, .rainy, and .cloudy. For each case, assign a message string describing the weather to a variable named message. Use these exact messages: "It's a bright sunny day!", "Don't forget your umbrella.", and "Looks a bit cloudy today." respectively.
Swift
Need a hint?

Use switch todayWeather { case .sunny: ... } and assign the exact messages to message.

4
Print the weather message
Write a print statement to display the message variable.
Swift
Need a hint?

Use print(message) to show the message on the screen.