Bird
Raised Fist0
Swiftprogramming~5 mins

Raw values for enums in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a raw value in a Swift enum?
A raw value is a constant value of a specific type (like String or Int) that each enum case can have. It lets you assign a fixed value to each case.
Click to reveal answer
beginner
How do you declare an enum with raw values in Swift?
You add a type after the enum name, like enum Direction: String, then assign raw values to each case, e.g., case north = "N".
Click to reveal answer
intermediate
Can raw values be of any type in Swift enums?
No, raw values must be of a literal type like String, Int, or Character. You cannot use complex types like arrays or dictionaries as raw values.
Click to reveal answer
beginner
How do you access the raw value of an enum case?
Use the .rawValue property on the enum case. For example, Direction.north.rawValue returns the raw value assigned to north.
Click to reveal answer
intermediate
How can you create an enum case from a raw value?
Use the initializer EnumType(rawValue: value). It returns an optional enum case matching the raw value or nil if no match is found.
Click to reveal answer
Which of these is a valid raw value type for a Swift enum?
AArray
BString
CDictionary
DClass
How do you declare an enum with Int raw values starting from 1?
Aenum Number: String { case one = "1", two, three }
Benum Number { case one = 1, two, three }
Cenum Number: Int { case one = 1, two, three }
Denum Number: Int { case one, two, three }
What does Direction(rawValue: "N") return if Direction enum has a case with raw value "N"?
AThe enum case matching "N"
Bnil
CA string "N"
DAn error
What is the type of Direction.north.rawValue if Direction is declared as enum Direction: String?
AString
BDirection
CInt
DOptional String
Can you assign different raw value types to different cases in the same enum?
AOnly if the enum is generic
BYes, each case can have its own type
COnly if you use associated values
DNo, all cases must have the same raw value type
Explain what raw values are in Swift enums and how to use them.
Think about fixed values assigned to enum cases.
You got /4 concepts.
    Describe the difference between raw values and associated values in Swift enums.
    Consider how each enum case stores data.
    You got /4 concepts.

      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

      1. Step 1: Understand what raw values are

        Raw values are fixed values assigned to enum cases, like strings or numbers.
      2. Step 2: Identify the purpose of raw values

        They help convert enum cases to simple data types for saving or displaying.
      3. Final Answer:

        To assign fixed simple values like strings or numbers to enum cases -> Option B
      4. 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

      1. Step 1: Check enum declaration with raw values

        Raw values require specifying the type after enum name, e.g., : String.
      2. Step 2: Verify correct case syntax

        Cases use 'case name = value' syntax, separated by new lines or semicolons.
      3. Final Answer:

        enum Direction: String { case north = "N" case south = "S" } -> Option C
      4. 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

      1. Step 1: Understand raw value auto-increment

        When the first case has a raw value (1), following cases auto-increment by 1.
      2. Step 2: Identify raw value of 'tuesday'

        Since monday = 1, tuesday = 2, wednesday = 3.
      3. Final Answer:

        2 -> Option D
      4. 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

      1. Step 1: Check raw value type consistency

        The enum declares raw type String, so all raw values must be strings.
      2. Step 2: Identify the incorrect raw value

        Case 'red' has raw value 1 (an Int), which conflicts with String type.
      3. Final Answer:

        Raw values must all be of the declared type String -> Option A
      4. 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))
      What is the output?
      hard
      A. Not Found Unknown
      B. OK Unknown
      C. Not Found Server Error
      D. Error at runtime

      Solution

      1. Step 1: Understand rawValue initializer

        StatusCode(rawValue: 404) returns .notFound; 201 returns nil.
      2. Step 2: Trace function output for each input

        For 404, returns "Not Found"; for 201, returns "Unknown".
      3. Final Answer:

        Not Found Unknown -> Option A
      4. Quick Check:

        Raw value init maps code to enum or nil [OK]
      Hint: Raw value init returns nil if no matching case [OK]
      Common Mistakes:
      • Assuming rawValue init always returns a case
      • Confusing rawValue with case index
      • Expecting runtime error for unknown raw value
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard