xcode 我的应用程序关闭后保存变量(swift)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31923410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save variable after my app closes (swift)
提问by lucas rodriguez
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:
我正在尝试在 Xcode 中保存一个变量,以便即使在应用程序关闭后它也能保存,但是当我访问它时,我是从几个不同的类和文件中进行的,并且在访问它时更改了变量的值. 因此类似的线程并不完全适用,要存储的值是一个字符串,这是我到目前为止的代码:
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.
无论如何,任何帮助将不胜感激。
回答by vadian
First of all you have to specify an unique key to store the variable (in the example MyKey
).
首先,您必须指定一个唯一键来存储变量(在示例中MyKey
)。
In 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.
在AppDelegate > applicationDidFinishLaunching
注册具有默认值的密钥。
好处是您可以使用非可选变量并且您有一个定义的默认值。
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")
回答by Test
Swift 3
斯威夫特 3
(thanks to vadian's answer)
(感谢瓦迪安的回答)
In AppDelegate > applicationDidFinishLaunching
:
在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")
回答by Mattias
Let's say you have a string variable called token
假设您有一个名为的字符串变量 token
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.
在上面的代码中,我确保给键一个值(“mytoken”)。这样我们以后就可以找到它。
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).
由于该方法objectForKey
返回一个 optional AnyObject
,我将确保将其强制转换为一个 optional String
(可选意味着它要么为零要么有值)。
回答by Yedidya Reiss
Add default.synchronize()
after setting value.
default.synchronize()
设定值后添加。