Bird
0
0

Which of the following enum case declarations correctly uses an associated value in Swift?

easy📝 Conceptual Q2 of 15
iOS Swift - Swift Language Essentials
Which of the following enum case declarations correctly uses an associated value in Swift?
Acase status: Bool
Bcase error = Int
Ccase message(text: String)
Dcase count -> Int
Step-by-Step Solution
Solution:
  1. Step 1: Review correct syntax for associated values

    In Swift, an enum case with associated values is declared as case name(Type) or with labels like case name(label: Type).
  2. Step 2: Analyze each option

    case message(text: String) uses labeled associated value syntax correctly. Options B, C, and D use invalid syntax for enum cases.
  3. Final Answer:

    case message(text: String) -> Option C
  4. Quick Check:

    Associated values use parentheses and optional labels [OK]
Quick Trick: Use parentheses for associated values in enum cases [OK]
Common Mistakes:
  • Using equal sign instead of parentheses
  • Using colon instead of parentheses
  • Trying to assign raw values without rawValue enum

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes