0

I think this is a simple problem, so you might go straight to the question.

I have a global variable, switchStatusIndex, (does not belong to any class.):

import Foundation

//Default
var userIndex:[Int] = [0,1,2]

var userIndexForItem:[Int] = [Int]()

//Index to hold the switch status for all datatypes
var switchStatusIndex:[String] = [
        NSUserDefaults.standardUserDefaults().boolForKey("switchA").description,
        NSUserDefaults.standardUserDefaults().boolForKey("switchB").description,
        NSUserDefaults.standardUserDefaults().boolForKey("switchC").description]

In ViewControllerOne I have a function:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    setupUserIndexForItem ()
    }

func setupUserIndexForItem () {
    userIndexForItem = [Int]()   //clears userIndexForItem

    for i in 0...userIndex.count-1{
        if switchStatusIndex[i] == "true"{
            userIndexForItem.append(userIndex[i])
        }
    }
}

Finally I have a ViewControllerTwo with UISwitches ex

@IBOutlet var switchA: UISwitch!

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}


override func layoutSubviews() {
    restoreSwitchesStates()
}


@IBAction func setStateSwitchA(sender: UISwitch) {
    var userdefault: NSUserDefaults = NSUserDefaults.standardUserDefaults()

    if switchA.on == true {
        userdefault.setBool(switchA!.on, forKey: "switchA")

    }else if switchA.on == false {
        userdefault.setBool(switchA!.on, forKey: "switchA")
    }
    userdefault.synchronize()
}

func restoreSwitchesStates() {
    switchA!.on = NSUserDefaults.standardUserDefaults().boolForKey("switchA")
}

When I flip the switch to .on, it updates NSUserdefault to "true". However, when I go back to ViewControllerOne, setupUserIndexForItem does not reflect this change. It is as switchStatusIndex does not update.
Question: How can I force a global variable to "update" ?

KML
  • 2,302
  • 5
  • 26
  • 47
  • Your logic & understanding of global variable is incorrect. Google it and see how they work - iOS global variable – Sam B Apr 21 '15 at 14:59
  • http://stackoverflow.com/questions/3601341/iphone-global-variable – Sam B Apr 21 '15 at 15:05
  • 1
    Use a segue from ViewControllerTwo to change the variable value in ViewControllerOne. Or set up a delegate method. – Shades Apr 21 '15 at 15:06
  • Why don't you use `NSUserDefaults` directly instead of wrapping it in another variable? That's not how it is intended to work. If you follow the documentation from Apple your values will always be the correct ones. – koen Apr 21 '15 at 16:36

2 Answers2

0

You can do this by many way.you can take switchStatusArray as global by putting in Appdelegate or you can use nsnotifiation or you can make delegate.

If use notification then first make method to intialize that array and use as a selector in notification method.

NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "intializeSwitcharrayMethod:",
name:"intializeSwitcharray" object: nil)

     @objc func  intializeSwitcharrayMethod(notification: NSNotification){     
    //do stuff

         Self.intializeSwitcharraywithUserdefaults()
     }


  func intializeSwitcharraywithUserdefaults()
 { 
    // your code of array intialization
  }

You can call intializeSwitcharraywithUserdefaults() from viewdidload also.

Bhoomi Jagani
  • 2,413
  • 18
  • 24
0

If I understand correctly your code, you're expecting the contents of the switchStatusIndex array to be updated every time you access that array from within the viewWillAppear method. But that's not going to happen because switchStatusIndex is set only once and never changed again. Perhaps what you intended was to define switchStatusIndex as a computed property or a property that is initialised by a closure, so that every time you access it, something happens (the array contents get updated with the values in NSUserDefaults).

wltrup
  • 778
  • 1
  • 4
  • 14
  • exactly: every time you access it, the array contents get updated with the values in NSUserDefaults. how do I do that? – KML Apr 21 '15 at 15:13
  • 1
    I think the explanations and examples in the [_Properties_](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID254) section of _The Swift Programming Language_ guide are very clear, and a short read. – wltrup Apr 21 '15 at 15:16