arrays - 保存用户数据时索引超出范围 - Swift
问题描述
作为序言,我是编程和 Swift 的新手。
我正在学习教程并学习保存用户数据。只有启用了保存编码器/解码器功能时才会发生此崩溃。
这是编码和解码:
override func encode(with coder: NSCoder) {
coder.encode(clickCount, forKey: "clickCount")
}
required init?(coder: NSCoder) {
super.init(coder: coder)
clickCount = coder.decodeObject(forKey: "clickCount") as? [Int] ?? [Int]()
}
我正在尝试在 clickCount 数组的 indexPath.row 处增加数组,以了解用户在我的 tableView 中单击单元格的次数
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(identifier: "Detail") as? DetailViewController {
vc.selectedImage = pictures[indexPath.row]
vc.totalPictures = pictures.count
vc.selectedPictureNumber = (pictures.firstIndex(of: vc.selectedImage ?? "Error") ?? 0) + 1
clickCount[indexPath.row] += 1
navigationController?.pushViewController(vc, animated: true)
}
}
最后,这是 clickCount 数组
var clickCount = [0,0,0,0,0,0,0,0,0,0]
需要明确的是,当我注释掉编码器和解码器功能时,递增数组按预期工作,但我想保存会话之间每行被点击的次数。
解决方案
推荐阅读
- javascript - 无法在自定义 BetterDiscord 扩展代码中解构“TouchBar”的属性“TouchBarButton”
- informatica - 使用来自 wsdl 的数据并通过 Informatica 云将数据公开为 Rest 或 soap,以供 Salesforce 应用程序使用
- c++ - 在 c++ 句子中显示单词
- java - 下载文件时如何处理 URL 中的日文文件名。在 IE 和 FireFox 中面临的问题
- java - 表示应将其删除的令牌上的语法错误
- apache-kafka - 使用 Avro 和使用 NiFi 的 Confluent 模式注册表将 JSON 中的数据写入 KAFKA
- r - 想知道如何在 `mapply` 中正确使用 `%>%`?
- javascript - javascript localStorage中可更改的HTML下拉菜单
- c++ - 将模板函数传递给模板函数(Eigen Derived)
- c++ - 进行统一构建时的内联行为(clang)