Saving Variables on app exit

后端 未结 1 1267
温柔的废话
温柔的废话 2021-01-24 07:33

I have a simple iPhone app in swift with multiple views and a list of 5 different items that have variables attached to them that get passed to each new view no matter where the

相关标签:
1条回答
  • 2021-01-24 08:06

    You can use NSUserDefaults and save the variable-values in it:

    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(Person1, forKey: "Person1")
    defaults.setInteger(Person1Age, forKey: "Person1Age")
    defaults.setBool(Person1Switch, forKey: "Person1Switch")
    

    Then you can get it later, even after an app restart like that:

    defaults.stringForKey("Person1")
    defaults.integerForKey("Person1Age")
    defaults.boolForKey("Person1Switch")
    

    As you see you can access it by using the fitting methods for your datatype.

    0 讨论(0)
提交回复
热议问题