Bird
0
0

Which of the following is a correct declaration of a @State variable for a Boolean named isOn?

easy📝 Conceptual Q2 of 15
iOS Swift - State Management in SwiftUI
Which of the following is a correct declaration of a @State variable for a Boolean named isOn?
Aprivate @State var isOn: Bool
B@State let isOn: Bool = true
Cvar @State isOn = true
D@State private var isOn = true
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct syntax for @State declaration

    The @State property wrapper is placed before the variable declaration, usually with private access.
  2. Step 2: Identify the correct syntax among options

    @State private var isOn = true uses '@State private var isOn = true' which is the correct and common pattern.
  3. Final Answer:

    @State private var isOn = true -> Option D
  4. Quick Check:

    @State declaration syntax = '@State private var' [OK]
Quick Trick: Declare @State as '@State private var variableName' [OK]
Common Mistakes:
  • Placing @State after var
  • Omitting 'var' keyword
  • Using @State without initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes