I want to get the current Boolean of the switch. how to do that in swift4 ?
@IBOutlet weak var switch_1: UISwitch!
var switch1_state: Bool = swich_1.get ???
I want to get the current Boolean of the switch. how to do that in swift4 ?
@IBOutlet weak var switch_1: UISwitch!
var switch1_state: Bool = swich_1.get ???
You can simply call switch.isOn
to get the state of the UISwitch
, but you should rather create an IBAction for your UISwitch
, which will be called every time the state of the Switch changes.
You can store the switch state in a computed property if you don't need to be notified every time the state of the switch changes or store the value in a stored property, whose value you can change from the connected IBAction.
var switchState:Bool {
return switch.isOn
}
You should also conform to the Swift naming convention, which is lowerCamelCase for function and variable names.
First of all please use camelCased rather than snake_cased variable names.
Second of all there's a typo – missing t
– in the second line.
And finally please use code completion (there is only one Bool
property) or read the documentation: