Save variable after my app closes (swift)
I’m trying to save a variable in Xcode so that it saves even after the app has closed, how ever when I access it I do it from a several different classes and files, and I change the value of the variable when I access it. Therefore similar threads do not completely apply, the value to be stored is a string and here is the code I have up until now:
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() defaults.setObject(Token, forKey: "") as! String
I believe this is the correct format, but I don’t know how to call it to change it because when I try I get an error message saying expected declaration.
Anyway any help would be very much appreciated.
- How to dismiss a view controller opened with presentModalViewController:
- CAEmitterLayer not rendering when -renderInContext: of superlayer is called
- iOS 8 Swift : How to keep the keyboard displayed when the UIAlercontroller is presented modally?
- Making custom back button for navigation bar
- ASIHTTPRequest vs AFNetworking vs NSUrlRequest
- How to get your own app version from xcode?
4 Solutions Collect From Internet About “Save variable after my app closes (swift)”
First of all you have to specify an unique key to store the variable (in the example
AppDelegate > applicationDidFinishLaunching register the key with a default value.
The benefit is you can use a non-optional variable and you have a defined default value.
let defaults = UserDefaults.standard let defaultValue = ["MyKey" : ""] defaults.register(defaults: defaultValue)
Now you can from everywhere read the value for the key
let defaults = UserDefaults.standard let token = defaults.string(forKey: "MyKey")
and save it
let defaults = UserDefaults.standard defaults.set(token, forKey: "MyKey")
Let’s say you have a string variable called
To save/update the stored value on the device:
NSUserDefaults.standardUserDefaults().setObject(token, forKey: "mytoken") NSUserDefaults.standardUserDefaults().synchronize()
In the code above I made sure to give the key a value (“mytoken”). This is so that we later can find it.
To read the stored value from the device:
let token = NSUserDefaults.standardUserDefaults().objectForKey("mytoken") as? String
Since the method
objectForKey returns an optional
AnyObject, I’ll make sure to cast it to an optional
String (optional meaning that it’s either nil or has a value).
(thanks to vadian’s answer)
AppDelegate > applicationDidFinishLaunching :
let defaults = UserDefaults.standard let defaultValue = ["myKey" : ""] defaults.register(defaults: defaultValue)
to save a key:
let defaults = UserDefaults.standard defaults.set("someVariableOrString", forKey: "myKey") defaults.synchronize()
to read a key:
let defaults = UserDefaults.standard let token = defaults.string(forKey: "myKey")
default.synchronize() after setting value.
- HTML5 Canvas drawImage ratio bug iOS
- how to use otool
- Easiest way to truncate float to 2 decimal places?
- prepareForSegue and PerformSegueWithIdentifier sender
- How to get Date from String in swift 3?
- App crashes after updating CoreData model that is being displayed in a UITableView
- UITextView editable on iOS5 but not iOS6
- How to take screenshot of portion of UIView?
- How to install cocoa pods?
- Exporting videos on iOS: understanding and setting frame duration property?
- iOS update to 10.3.1 breaks HTML input element
- Non-'@objc' method does not satisfy optional requirement of '@objc' protocol
- How to access CoreData model in today extension (iOS)
- Code Signing issue for Project with Multiple Targets
- How to use SocketRocket with socket.io?