gpt4 book ai didi

ios - 用户默认设置不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:53 25 4
gpt4 key购买 nike

有人能帮我解释为什么我不能正确执行 nsuserdefaults 吗?当我尝试点击保存时,它什么也不做。我想要的是当我点击保存时它会保存到手机中,然后当我再次尝试返回 View Controller 时它会出现。下面是我的代码。

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.setHidesBackButton(true, animated: false)
self.hideKeyboardWhenTappedAround()

firstName.delegate = self
lastName.delegate = self

tableView.reloadData()

//Save Defaults
let defaults = UserDefaults.standard
if let _ = defaults.object(forKey: "firstName") as? String {
self.firstName.text = defaults.object(forKey: "firstName") as? String
}

if let _ = defaults.object(forKey: "lastName") as? String {
self.lastName.text = defaults.object(forKey: "lastName") as? String
}

if let _ = defaults.object(forKey: "contacts") as? Array<Any> {
self.contacts = (defaults.object(forKey: "contacts") as? Array)!
}
}



//MARK -> SAVE BUTTON
@IBAction func saveButtonSelected(_ sender: AnyObject) {
let defaults: UserDefaults = UserDefaults.standard
defaults.set(self.firstName.text, forKey: "firstName")
defaults.set(self.lastName.text, forKey: "lastName")
defaults.set(contacts, forKey: "contacts")
}

最佳答案

试试这个:

 @IBAction func saveButtonSelected(_ sender: AnyObject) {
let defaults: UserDefaults = UserDefaults.standard
defaults.set(self.firstName.text, forKey: "firstName")
defaults.set(self.lastName.text, forKey: "lastName")
defaults.set(contacts, forKey: "contacts")
defaults.synchronize()
}

关于ios - 用户默认设置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40969162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com