【发布时间】:2019-09-15 09:17:15
【问题描述】:
我的项目有一个带有 UITextField 的警报按钮,允许我输入一个字符串,然后将其附加到我已全局声明的数组中。此添加允许另一个 UITextField 在其下拉菜单中显示添加。但是,更改只会在应用程序保持打开状态时保存,并且在我尝试配置 UserDefaults 时不会保存。
我已经阅读了类似的 S.O.帖子,但我无法获得任何解决我的问题的解决方案。
(这是全局声明的。)
let defaults = UserDefaults.standard
(这也是全局的。)
var needIndicatorArray: [String] = ["SNAP", "EBT", "FVRX"]
(这是我用来附加上述数组的代码。此代码将附加数组,但在应用程序关闭并重新打开后不会保存添加。)
@IBAction func addNeedIndicator(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add Need Indicator", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
//This should append the global array once the user hits the add item on the UIAlert
self.needIndicatorArray.append(textField.text!)
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new item"
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
【问题讨论】:
标签: arrays swift append nsuserdefaults