gpt4 book ai didi

arrays - Swift 3 使用 UserDefaults 保存数组

转载 作者:行者123 更新时间:2023-11-28 09:25:44 30 4
gpt4 key购买 nike

我正在尝试将数组保存到 UserDefaults,但在运行应用程序后显示此 Error .当应用程序崩溃时,Xcode 会显示正确的数组值,因此数据会很好地保存到数组中。我将字符串数据保存到第一个 VC 中的数组:

 Last20Times.insert(dsa, at: 0) // Last20Times is the array.
if Last20Times.count > 0 {
let defaults = UserDefaults.standard
defaults.set(Last20Times, forKey: "last20")

第二个 Controller 是 TableView:

 if indexPath.section == 1 {
let ArrayLast20 = UserDefaults.standard.stringArray(forKey: "last20")
cell.textLabel?.text = ArrayLast20?[indexPath.row]// error(picture)
}

return cell

谢谢

最佳答案

您需要这样保存您的 UserDefaults:

defaults.synchronize() 

. . .

关于arrays - Swift 3 使用 UserDefaults 保存数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41192341/

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