In a UIControl
, if I override isHighlighted
to set a private _isHighlighted
property, and then check the control's state
to see if it contains .highlighted
, the state still accurately reflects the change. See code below.
My question is, how is this possible? I never call super.isHighlighted
or manipulate the state property. state
is an OptionSet
that must have the .highlighted
property inserted into the set, which, from what I can determine, does not, or should not, happen if I override the property.
The only other explanation I can think of is that state
is actually a computed property based off other properties (i.e. isSelected
, isEnabled
, etc.)
class MyControl: UIControl {
private var _isHighlighted: Bool = false
override var isHighlighted: Bool {
get { return self._isHighlighted }
set { self._isHighlighted = newValue }
}
}
let myControl = MyControl()
myControl.isHighlighted = true
myControl.state.contains(.highlighted) // returns true
Good explanation! Let's try logging (printing) in the getter to see if that's true:
And here's the log:
Quod erat demonstrandum.