📖 Scenario: Imagine you are creating a simple app that needs to represent different types of fruits with specific codes. You want to use Swift enums with raw values to assign each fruit a unique code.
🎯 Goal: Build a Swift program that defines an enum with raw values for fruits and prints the raw value of a selected fruit.
📋 What You'll Learn
Create an enum called Fruit with raw values of type String
Include these fruits with exact raw values: apple = "A01", banana = "B02", cherry = "C03"
Create a variable called selectedFruit and assign it the enum case .banana
Print the raw value of selectedFruit using print
💡 Why This Matters
🌍 Real World
Enums with raw values are useful for representing fixed sets of related values with associated codes, like product categories, error codes, or status flags.
💼 Career
Understanding enums with raw values is important for Swift developers working on iOS apps, as it helps organize data clearly and safely.
Progress0 / 4 steps
1
Create the Fruit enum with raw values
Create an enum called Fruit with raw values of type String. Add these cases with exact raw values: apple = "A01", banana = "B02", cherry = "C03".
Swift
Hint
Use enum Fruit: String and assign raw values with =.
2
Create a variable for the selected fruit
Create a variable called selectedFruit and assign it the enum case .banana from the Fruit enum.
Swift
Hint
Use var selectedFruit = Fruit.banana to assign the enum case.
3
Access the raw value of the selected fruit
Create a constant called fruitCode and assign it the raw value of selectedFruit using the rawValue property.
Swift
Hint
Use let fruitCode = selectedFruit.rawValue to get the raw value string.
4
Print the raw value of the selected fruit
Use print to display the value of fruitCode.
Swift
Hint
Use print(fruitCode) to show the raw value.
Practice
(1/5)
1. What is the purpose of raw values in Swift enums?
easy
A. To allow enums to inherit from classes
B. To assign fixed simple values like strings or numbers to enum cases
C. To make enums mutable after declaration
D. To create functions inside enums
Solution
Step 1: Understand what raw values are
Raw values are fixed values assigned to enum cases, like strings or numbers.
Step 2: Identify the purpose of raw values
They help convert enum cases to simple data types for saving or displaying.
Final Answer:
To assign fixed simple values like strings or numbers to enum cases -> Option B
Quick Check:
Raw values = fixed simple values [OK]
Hint: Raw values give enums fixed simple data like strings or numbers [OK]
Common Mistakes:
Thinking raw values make enums mutable
Confusing raw values with associated values
Believing enums can inherit from classes
2. Which of the following is the correct syntax to declare an enum with raw values in Swift?
easy
A. enum Direction: Int { case north; case south }
B. enum Direction { case north = "N" case south = "S" }
C. enum Direction: String { case north = "N" case south = "S" }
D. enum Direction: String { north = "N", south = "S" }
Solution
Step 1: Check enum declaration with raw values
Raw values require specifying the type after enum name, e.g., : String.
Step 2: Verify correct case syntax
Cases use 'case name = value' syntax, separated by new lines or semicolons.
Final Answer:
enum Direction: String { case north = "N" case south = "S" } -> Option C
Quick Check:
Raw value enum syntax = enum Direction: String { case north = "N" case south = "S" } [OK]
Hint: Raw value enums need type after name and '=' for values [OK]
Common Mistakes:
Omitting the raw value type after enum name
Using commas instead of separate case lines
Assigning raw values without specifying type
3. What will be the output of this Swift code?
enum Weekday: Int {
case monday = 1
case tuesday
case wednesday
}
print(Weekday.tuesday.rawValue)
medium
A. 1
B. Error: rawValue not found
C. 3
D. 2
Solution
Step 1: Understand raw value auto-increment
When the first case has a raw value (1), following cases auto-increment by 1.
Step 2: Identify raw value of 'tuesday'
Since monday = 1, tuesday = 2, wednesday = 3.
Final Answer:
2 -> Option D
Quick Check:
Auto-increment raw values = 2 [OK]
Hint: Raw Int values auto-increment from first assigned value [OK]
Common Mistakes:
Assuming all raw values must be explicitly assigned
Confusing rawValue with case index
Expecting an error when accessing rawValue
4. Identify the error in this enum declaration:
enum Color: String {
case red = 1
case green = "Green"
case blue = "Blue"
}
medium
A. Raw values must all be of the declared type String
B. Enum cases cannot have raw values
C. Raw values must be integers only
D. Missing semicolon after each case
Solution
Step 1: Check raw value type consistency
The enum declares raw type String, so all raw values must be strings.
Step 2: Identify the incorrect raw value
Case 'red' has raw value 1 (an Int), which conflicts with String type.
Final Answer:
Raw values must all be of the declared type String -> Option A
Quick Check:
Raw values must match enum type [OK]
Hint: All raw values must match enum's declared raw type [OK]
Common Mistakes:
Mixing raw value types in one enum
Thinking semicolons are required after cases
Believing enums can't have raw values
5. Given this enum:
enum StatusCode: Int {
case success = 200
case notFound = 404
case serverError = 500
}
func message(for code: Int) -> String {
if let status = StatusCode(rawValue: code) {
switch status {
case .success: return "OK"
case .notFound: return "Not Found"
case .serverError: return "Server Error"
}
} else {
return "Unknown"
}
}
print(message(for: 404))
print(message(for: 201))