Bird
0
0

How do you instantiate a UISwitch in Swift and set its state to ON immediately after creation?

easy📝 Syntax Q3 of 15
iOS Swift - User Input and Forms
How do you instantiate a UISwitch in Swift and set its state to ON immediately after creation?
Alet toggle = UISwitch(); toggle.setOn(false, animated: true)
Blet toggle = UISwitch(isOn: true)
Clet toggle = UISwitch(on: true)
Dlet toggle = UISwitch(); toggle.isOn = true
Step-by-Step Solution
Solution:
  1. Step 1: Create UISwitch instance

    Use UISwitch() to instantiate.
  2. Step 2: Set the switch ON

    Set isOn property to true to turn it ON.
  3. Step 3: Validate other options

    Options B and C use invalid initializers; D sets it OFF.
  4. Final Answer:

    let toggle = UISwitch(); toggle.isOn = true -> Option D
  5. Quick Check:

    Use default init then set isOn [OK]
Quick Trick: Instantiate UISwitch then set isOn property [OK]
Common Mistakes:
  • Trying to use non-existent initializers
  • Using setOn(false) instead of true

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes