gpt4 book ai didi

swift - Tableview 数据不会使用 NSUserDefaults 打印

转载 作者:行者123 更新时间:2023-11-30 13:55:27 24 4
gpt4 key购买 nike

我正在尝试在 NSUserDefaults 中获取保存的数组并将其添加到我的 TableView 中...当我运行 print(notifications[indexPath.row]) 时,我的数组(通知)内容将被打印。但是,当我将这些内容添加到我的表格 View 中时,什么也没有显示。我假设虽然数组是通过 NSUserDefaults 保存和检索的,但它可能无法将数组内容转换为可以添加到 TableView 中的字符串。任何帮助,将不胜感激。谢谢!

这里是一些可能有用的额外代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = notificationsTableView.dequeueReusableCellWithIdentifier("notificationsCell") as! NotificationsTableViewCell
//Set notificationsLabel of cells
print(notifications[indexPath.row])
cell.notificationsLabel.text = notifications[indexPath.row] as? String
//Set datesLabel of cells
cell.datesLabel.text = dates[indexPath.row] as? String

return cell
}

最佳答案

修复:我必须指定数组仅由String组成。为此,我通过 var notification = [NSString]() 初始化了数组。然后我可以使用 append 命令来编辑该数组。最后,要将保存的内容添加到我运行的数组中,notifications = NSUserDefaults.standardUserDefaults().objectForKey("notificationsKey") as! [NSString]

关于swift - Tableview 数据不会使用 NSUserDefaults 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684665/

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