Bird
0
0

What is wrong with this SwiftUI code?

medium📝 Debug Q14 of 15
iOS Swift - State Management in SwiftUI
What is wrong with this SwiftUI code?
struct ExampleView: View {
  @State var text: String
  var body: some View {
    Text(text)
  }
}
AThe @State variable must have an initial value
BText cannot display a String variable
CThe body property must be a function
DThe struct must inherit from UIViewController
Step-by-Step Solution
Solution:
  1. Step 1: Check @State variable declaration

    @State variables must have an initial value because SwiftUI manages their storage.
  2. Step 2: Verify other parts

    Text can display String, body is a computed property, and struct inherits from View, not UIViewController.
  3. Final Answer:

    The @State variable must have an initial value -> Option A
  4. Quick Check:

    @State needs initial value = B [OK]
Quick Trick: Always initialize @State variables when declaring [OK]
Common Mistakes:
  • Leaving @State variables uninitialized
  • Confusing View with UIViewController
  • Misunderstanding Text view usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes