# Use switch as global variable on multiple classes in Swift 4

**URL:** <https://forums.kodeco.com/t/use-switch-as-global-variable-on-multiple-classes-in-swift-4/54859>\
**Category:** Swift\
**Created:** [November 7, 2018, 3:37pm UTC](https://forums.kodeco.com/t/use-switch-as-global-variable-on-multiple-classes-in-swift-4/54859 "2018-11-07T15:37:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [November 7, 2018, 3:37pm UTC](https://forums.kodeco.com/t/use-switch-as-global-variable-on-multiple-classes-in-swift-4/54859/1 "2018-11-07T15:37:31Z")

</div>

I am trying to use a global variable to determine the background color of multiple classes. So if the switch in settingsViewController is on the background color should be red if it’s off it should be blue. I am also user defaults to try to save the value of the global var.

```
         var global = UserDefaults.standard.string(forKey: "sx")
    import UIKit

     class settingsViewController: UIViewController {

  var total = ""

   @IBOutlet var switchy : UISwitch!

   override func viewDidLoad() {
super.viewDidLoad()

       if global = 2{
      view.backgroundcolor = uicolor.red
    } else {
  view.backgroundcolor = uicolor.blue
       }

    }

  @IBAction func pressONorOff(){
if switchy.isOn == true {
    global = String(2)
 UserDefaults.standard.set("2", forKey: "sx")

} else {
    global = String(1)
    UserDefaults.standard.set("1", forKey: "sx")
}
  }}

  class twoVC: UIViewController {

 override func viewDidLoad() {
super.viewDidLoad()

       if global = 2{
      view.backgroundcolor = uicolor.red
    } else {
   view.backgroundcolor = uicolor.blue
       }}}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [November 10, 2018, 6:34am UTC](https://forums.kodeco.com/t/use-switch-as-global-variable-on-multiple-classes-in-swift-4/54859/2 "2018-11-10T06:34:11Z")

</div>

Hi @timswift,  
maybe it is that when you typed the code here, you have an error, look at your if loops,

```auto
 if global = 2 {

```

This is invalid as you are trying to compare not assign, you would need

```auto
if global == 2 {

```

Secondly, you need to update the backgrounds of your views when you trigger the switch and change the values.

cheers,

Jayant

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 23, 2019, 7:37am UTC](https://forums.kodeco.com/t/use-switch-as-global-variable-on-multiple-classes-in-swift-4/54859/3 "2019-04-23T07:37:32Z")

</div>

Closed on April 23, 2019, 7:37am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
