Raw values for enums
📖 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 StringInclude these fruits with exact raw values:
apple = "A01", banana = "B02", cherry = "C03"Create a variable called
selectedFruit and assign it the enum case .bananaPrint 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