user3798602 user3798602 - 4 months ago 35
Swift Question

saving key and value pairs swift

I need a way to save key and value pairs in swift for my application. I am new to IOS programming, but I know Android and I am looking for something like shared preferences in android, but for swift. By "save," I mean that when the user quits out of the application, the key and value pairs are saved in the local data.


What you are looking for is called "Userdefaults" - you can use them like this:

Saving Data:

let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(variable, forKey: "yourKey")

Reading Data:

if let yourVariable: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("yourKey") {

Here you find more information: