简体   繁体   English

有没有一种方法可以处理NSUserDefaults存储的信息

[英]Is there a way to manipulate NSUserDefaults stored information

I have stored a dictionary into NSUserDefaults like this: 我已经将字典存储到NSUserDefaults中,如下所示:

standardDefaults.setObject(switchStater, forKey: "startUp") the switchStater object is the dictionary that stores the rowIndex and the Bool value for each UISwitch . standardDefaults.setObject(switchStater, forKey: "startUp") switchStater对象是存储每个UISwitchrowIndexBool值的字典。 I can print out the information that is stored in the NSUserDefaults like this: 我可以像这样打印出存储在NSUserDefaults的信息:

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 the UISwitch when they are manipulated. 我将.setObject放在UITableViewController的子目录中,因为子类在对其进行操作时会编写UISwitch I then call the .objectForKey in the viewDidLoad of the superclass. 然后,我在超类的viewDidLoad中调用.objectForKey

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? 有没有一种方法可以测试存储在for init循环中的NSUserDefault如果没有),我该如何测试测试,以便UISwitch处于用户重新加载应用程序后的位置?

Thanks for the help. 谢谢您的帮助。

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: 您可以使用userDefaults.dictionaryForKey("startUp")NSUserDefaults检索存储的Dictionary ,然后遍历字典中的键/值,并按如下所示适当地设置UISwitch

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM