Rico
Rico

Reputation: 59

Is there a way to manipulate NSUserDefaults stored information

I have stored a dictionary into NSUserDefaults like this:

standardDefaults.setObject(switchStater, forKey: "startUp") the switchStater object is the dictionary that stores the rowIndex and the Bool value for each UISwitch. I can print out the information that is stored in the NSUserDefaults like this:

let test = standardDefaults.objectForKey("startUp")
        print(test)

This is how I confirm the data is stored properly. I put the .setObject in the sublass of the UITableViewController because the subclass writes theUISwitch when they are manipulated. I then call the .objectForKey in the viewDidLoad of the superclass.

Is there a way to test the NSUserDefault stored with a for init loop if not how can I test the test so the UISwitch is in the position it was left after the user reloads the app?

Thanks for the help.

Upvotes: 1

Views: 94

Answers (1)

tcarey
tcarey

Reputation: 311

You can retrieve the stored Dictionary from NSUserDefaults with userDefaults.dictionaryForKey("startUp") then loop through the key/values in the dictionary and set the UISwitch appropriately like this:

let dictionary = userDefaults.dictionaryForKey("startUp")!

for (key, value): (String, AnyObject) in dictionary {
    let switch = value as! UISwitch
    print(key)
    print(switch.on)
}

Upvotes: 2

Related Questions