我需要存储一些变量,如ione和itwo,用于方法重复。 变量值会增加,增加后,我想将增加的变量存储到userdefaults中。
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
/* How can I load UserDefaults here to restore ione's value and
itwo's value and How to store them in UserDefaults ? */
let ionevalue = ione
println(ionevalue)
let itwovalue = itwo
println(itwovalue)
// Do any additional setup after loading the view, typically from a nib.
}
var ione = 0
var itwo = 0
ione++
itwo++
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
let aaa = NSUserDefaults.standardUserDefaults()
并使用以下代码存储更新值:
aaa.setObject(AnythingYouInitialed, forKey: "DefineAKeyYouWant")
并使用以下代码加载NSUserDefaults中的值:
if let bbb = aaa.valueForKey("DefineAKeyYouWant") as?Int
{
ccc = bbb
}
现在我加载了值,我可以通过将ccc分配给其他变量来使用它。
我是一个30岁的老家伙,我刚开始做一名程序员,这对我来说并不容易......哈哈......