Bird
0
0

Given this code snippet, what will be printed when the button is tapped?

medium📝 Predict Output Q5 of 15
iOS Swift - SwiftUI Basics
Given this code snippet, what will be printed when the button is tapped?
let button = UIButton()
button.addTarget(self, action: #selector(buttonPressed(_:)), for: .touchUpInside)

@objc func buttonPressed(_ sender: UIButton) {
  print(sender.isEnabled)
}
Afalse
BThe app crashes
Cnil
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Understand sender parameter

    The sender is the button itself, so sender.isEnabled reflects the button's enabled state.
  2. Step 2: Default UIButton is enabled

    By default, UIButton's isEnabled property is true unless changed.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    UIButton is enabled by default = true [OK]
Quick Trick: UIButton is enabled by default unless disabled [OK]
Common Mistakes:
  • Assuming isEnabled is false by default
  • Expecting nil instead of Bool
  • Confusing sender with other objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes