【问题标题】:Why my local storage (Userdefaults) is empty every time I close the app completely (Swift)为什么每次我完全关闭应用程序时我的本地存储(Userdefaults)都是空的(Swift)
【发布时间】:2018-04-23 21:34:42
【问题描述】:

我在 UIViewController 中有一个类

class myArrayDataStructure {
    static var myArray = Array<String>() // the singleton array
    private init() {} // prevents unwanted access
}

我要求用户输入一个字符串并使用按钮存储它,如下所示:

 @IBAction func EnterAction(_ sender: Any) {
     if let sContent = TextF.text {
         // append it to the global singleton array
         myArrayDataStructure.myArray.append(sContent)
         UserDefaults.standard.set(myArrayDataStructure.myArray, forKey: "items")}

在另一个 viewController 中,我正在拉取数组并将其显示在 tableView 中。我的标识符单元格名为“Cell”:

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return myArrayDataStructure.myArray.count
    }

  let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
        let returnedItems = UserDefaults.standard.stringArray(forKey: "items") ?? [String]()
        let tempLabel = returnedItems[indexPath.row]
        cell.textLabel?.text = tempLabel + " Good Job"
        return cell

代码运行良好,我可以在表格中存储和显示项目,但是如果我双击主页按钮并重新打开应用程序,表格是空的。即使完全关闭应用程序,我如何才能将保存的项目保留在表格中。

override func viewDidAppear(_ animated: Bool) {
        let returnedItems = UserDefaults.standard.stringArray(forKey: "items") ?? [String]()
        for item in returnedItems{
            totalAmount.text = item + " Miles"    // totalAmount is a label under the table
        }

        print(returnedItems)
        table.reloadData()
    }

【问题讨论】:

  • 显示您的numberOfRowsInSection 方法。
  • 从您发布的代码中,您只在 UserDefaults 中设置了数组,但从未从中读取。
  • 现在展示如何从 UserDefaults 初始化 myArrayDataStructure.myArray。
  • @rmaddy 这就是我在第二个 viewController 中的全部内容。现在,当我关闭应用程序并重新打开时,我可以看到仍在表格中的数据。但是当我双击主页并完全关闭它时,数据就消失了。如果我还需要初始化任何东西,我不知道该怎么做。我感谢任何指导。
  • 那是因为当您的应用重新启动时,您从未从UserDefaults 加载数据。

标签: ios arrays swift local-storage


【解决方案1】:

您的主要问题是当您的应用重新加载时您没有填充 myArrayDataStructure.myArray。

你也没有很好地利用你的全局数组。您没有在 cellForRowAt 方法中使用。您没有在 viewDidAppear 方法中使用它。

您确实在 numberOfRowsInSection 和 EnterAction 方法中使用它。

您至少需要按如下方式更新您的代码:

首先将UserDefaults 加载到您的全局数组中:

class myArrayDataStructure {
    static var myArray = UserDefaults.standard.stringArray(forKey: "items") ?? [String]()
    private init() {} // prevents unwanted access
}

然后更新您的视图控制器以一致地使用全局数组:

你的cellForRowAt:

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let tempLabel = myArrayDataStructure.myArray[indexPath.row]
cell.textLabel?.text = tempLabel + " Good Job"
return cell

你的viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    for item in myArrayDataStructure.myArray {
        totalAmount.text = item + " Miles"
    }

    table.reloadData()
}

请注意,viewDidAppear 中的循环只会显示数组中的最后一个值。这有点无意义。

【讨论】:

    猜你喜欢
    • 2016-05-09
    • 2020-11-16
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多