在 Xcode Swift 上保存和加载整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27642492/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:24:41  来源:igfitidea点击:

Saving And Loading An Integer On Xcode Swift

iosxcodeswiftpersistencensuserdefaults

提问by SRL

I am trying to save an integer so it shows up after I switch the page or close the game. I made this to change the number but how do I save that number when I switch pages and load it when I go back to that page.

我正在尝试保存一个整数,以便在我切换页面或关闭游戏后显示。我这样做是为了更改数字,但是如何在切换页面时保存该数字并在返回该页面时加载它。

Change Code:

更改代码:

@IBAction func MoneyPress(sender: AnyObject) {

Money += 1

var MoneyNumberString:String = String(format: "Dollars:%i", Money)
self.DollarsLabel.text = (string: MoneyNumberString)

}

回答by Blake Morgan

If it isn't a lot of data, the strategy I use to save data, pass it between pages, and persist it between app runs is to store the value in NSUserDefaults.

如果数据不是很多,我用来保存数据、在页面之间传递数据并在应用程序运行之间保持数据的策略是将值存储在NSUserDefaults.

Setting A Value: When you first get or when you change the data, store it in NSUserDefaults.

设置值:当您第一次获取或更改数据时,将其存储在NSUserDefaults.

@IBAction func MoneyPress(sender: AnyObject) {
    Money += 1
    var MoneyNumberString:String = String(format: "Dollars:%i", Money)
    self.DollarsLabel.text = (string: MoneyNumberString)
    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //This class     variable needs to be defined every class where you set or fetch values from NSUserDefaults
    defaults.setObject(MoneyNumberString, forKey: "money")
    defaults.synchronize() //Call when you're done editing all defaults for the method.
}

Loading A Value: When you need to get the values, just grab it from NSUserDefaults.

加载值:当您需要获取值时,只需从NSUserDefaults.

@IBAction func loadButton(sender: UIButton) {
    let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
    var money = defaults.valueForKey("money") as? String
    dollarLabel.text! = money
}

To remove the stored data, all you need to do is call the removeObjectForKeyfunction for each key previously set.

要删除存储的数据,您只需为之前设置的每个键调用removeObjectForKey函数。

let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("money")
defaults.synchronize()

Helpful Source on NSUserDefaults:

NSUserDefaults 上的有用资源

NSUserDefulatsClass Reference: Link here.

NSUserDefulats课程参考:此处链接。

回答by Midhun MP

You can use NSUserDefaultsfor this.

您可以为此使用NSUserDefaults

Save Value

节省价值

NSUserDefaults.standardUserDefaults().setInteger(money, forKey: "MoneyKey");


Retrieve Value

检索值

NSUserDefaults.standardUserDefaults().integerForKey("MoneyKey");

So can retrieve the value in viewDidLoadand load the data:

因此可以检索值viewDidLoad并加载数据:

override func viewDidLoad()
{
    super.viewDidLoad()
    loadWebView()
    var money = NSUserDefaults.standardUserDefaults().integerForKey("MoneyKey");
}

When you come to the view for the first time the value of money will be 0.

当您第一次来到该视图时,钱的价值将为0



Remove Value

删除值

If you need to remove a value from NSUserdefaults, you can use:

如果需要从 中删除值NSUserdefaults,可以使用:

NSUserDefaults.standardUserDefaults().removeObjectForKey("MoneyKey")