-6

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 ???
jscs
  • 63,694
  • 13
  • 151
  • 195
user9212972
  • 39
  • 2
  • 8
  • 2
    Possible duplicate of [State of UISwitch](https://stackoverflow.com/questions/46899032/state-of-uiswitch) – jscs Mar 04 '18 at 16:27
  • 6
    Please take a moment to review the documentation for `UISwitch`. This is really trivial. – rmaddy Mar 04 '18 at 16:28

2 Answers2

6

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.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
0

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 Boolproperty) or read the documentation:

enter image description here

vadian
  • 274,689
  • 30
  • 353
  • 361