Bird
Raised Fist0
Swiftprogramming~10 mins

Associated values per case in Swift - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an enum case with an associated value.

Swift
enum Result {
    case success([1]: String)
}
Drag options to blanks, or click blank then click option'
Amessage
Bvalue
Cinfo
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type name instead of a variable name for the associated value.
Omitting the associated value name.
2fill in blank
medium

Complete the code to extract the associated value from the enum using a switch statement.

Swift
let result = Result.success(message: "Done")
switch result {
case .success(let [1]):
    print([1])
default:
    break
}
Drag options to blanks, or click blank then click option'
Ainfo
Bvalue
Cdata
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the associated value name.
Forgetting to use the variable in the print statement.
3fill in blank
hard

Fix the error in the enum definition with multiple associated values.

Swift
enum NetworkResponse {
    case error(code: Int, [1]: String)
}
Drag options to blanks, or click blank then click option'
Aerror
Bmessage
Cstatus
Ddescription
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type name instead of a variable name.
Using a name that conflicts with the first associated value.
4fill in blank
hard

Fill both blanks to create a function that returns the associated value from the enum.

Swift
func getMessage(from response: NetworkResponse) -> String? {
    switch response {
    case .error(_, let [1]):
        return [2]
    default:
        return nil
    }
}
Drag options to blanks, or click blank then click option'
Amessage
Berror
Ddescription
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for extraction and return.
Trying to return the ignored code value.
5fill in blank
hard

Fill all three blanks to define an enum with multiple cases and associated values, and create an instance.

Swift
enum Device {
    case phone([1]: String)
    case tablet([2]: Int)
    case laptop([3]: String)
}

let myDevice = Device.phone(model: "iPhone 14")
Drag options to blanks, or click blank then click option'
Amodel
Bsize
Dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent or duplicate names for associated values.
Not matching the associated value name when creating the instance.

Practice

(1/5)
1. What is the main purpose of associated values in Swift enums?
easy
A. To store extra data with each enum case
B. To define multiple enums in one declaration
C. To create functions inside enums
D. To inherit properties from other enums

Solution

  1. Step 1: Understand enum cases

    Enum cases represent fixed options, but sometimes need extra data.
  2. Step 2: Role of associated values

    Associated values let each case carry extra information, like a label or number.
  3. Final Answer:

    To store extra data with each enum case -> Option A
  4. Quick Check:

    Associated values = extra data per case [OK]
Hint: Think: extra info stored inside enum cases [OK]
Common Mistakes:
  • Confusing associated values with raw values
  • Thinking enums can inherit properties
  • Believing enums can contain functions only
2. Which of the following is the correct syntax to define an enum case with an associated value of type String?
easy
A. case name -> String
B. case name(String)
C. case name: String
D. case name = String

Solution

  1. Step 1: Recall enum case syntax with associated values

    Associated values use parentheses after the case name with the type inside.
  2. Step 2: Match syntax options

    Only 'case name(String)' uses parentheses correctly for associated values.
  3. Final Answer:

    case name(String) -> Option B
  4. Quick Check:

    Parentheses hold associated value type [OK]
Hint: Use parentheses for associated value types [OK]
Common Mistakes:
  • Using colon instead of parentheses
  • Using equals sign for associated values
  • Using arrow notation incorrectly
3. What will be the output of this Swift code?
enum Result {
    case success(String)
    case failure(Int)
}

let response = Result.success("Done")
switch response {
case .success(let message):
    print(message)
case .failure(let code):
    print("Error code: \(code)")
}
medium
A. Error code: Done
B. Compilation error
C. Error code: 0
D. Done

Solution

  1. Step 1: Identify enum case used

    The variable 'response' is assigned 'Result.success("Done")'.
  2. Step 2: Switch matches .success case

    The switch matches '.success(let message)' and prints the message string.
  3. Final Answer:

    Done -> Option D
  4. Quick Check:

    Matching case prints associated string [OK]
Hint: Match enum case to print its associated value [OK]
Common Mistakes:
  • Confusing success and failure cases
  • Expecting error code instead of message
  • Thinking code will not compile
4. Identify the error in this Swift enum usage:
enum Device {
    case phone(model: String)
    case tablet(model: String)
}

let myDevice = Device.phone("iPhone")
medium
A. Missing parentheses around associated value
B. Enum cases cannot have associated values
C. Cannot assign associated value without label
D. Incorrect enum declaration syntax

Solution

  1. Step 1: Check enum case declaration

    Cases use labeled associated values: 'phone(model: String)'.
  2. Step 2: Check enum instance assignment

    Assigning 'Device.phone("iPhone")' omits the label 'model:'.
  3. Final Answer:

    Cannot assign associated value without label -> Option C
  4. Quick Check:

    Labeled associated values require labels on assignment [OK]
Hint: Use labels when enum cases define labeled associated values [OK]
Common Mistakes:
  • Ignoring labels in associated values
  • Thinking parentheses are missing
  • Believing enums can't have associated values
5. Given this enum with multiple associated values:
enum Message {
    case text(String, Int)
    case image(url: String, size: (width: Int, height: Int))
}

let msg = Message.image(url: "photo.png", size: (width: 100, height: 200))
switch msg {
case .text(let content, let length):
    print("Text: \(content), length: \(length)")
case .image(let url, let size):
    print("Image URL: \(url), size: \(size.width)x\(size.height)")
}
What will be printed?
hard
A. Image URL: photo.png, size: 100x200
B. Text: photo.png, length: 100
C. Image URL: photo.png, size: (100, 200)
D. Compilation error due to tuple

Solution

  1. Step 1: Identify enum case and associated values

    Variable 'msg' is assigned '.image' with url and size tuple.
  2. Step 2: Switch matches .image case and prints values

    Print statement accesses url and tuple's width and height correctly.
  3. Final Answer:

    Image URL: photo.png, size: 100x200 -> Option A
  4. Quick Check:

    Tuple values accessed with dot notation [OK]
Hint: Access tuple parts with dot notation inside switch [OK]
Common Mistakes:
  • Printing tuple as whole instead of parts
  • Confusing .text and .image cases
  • Expecting compilation error from tuple
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