ios - 如何解决二维数组中的“线程 1:致命错误:索引超出范围”
问题描述
启动我的应用程序“线程 1:致命错误:索引超出范围”时发生崩溃。完整错误:
Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
2020-07-11 14:04:40.909080+0200 Carrot[13983:705959] Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
(lldb)
谁能告诉我如何解决这个错误?我在这一行收到错误:
return twoDArray[section].items.count
这是我完整的 ViewController 文件:
class TableViewController: UITableViewController, AddTask, ChangeButton {
var sections = FoodData.foodCategories
var twoDArray = [
Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]), Section(isExpanded: true, items: [Task(name: "No items yet")]),
]
// MARK: - TableView data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
}
return twoDArray[section].items.count
}
这是 FoodData 结构:
struct FoodData {
static let foodCategories = ["New item", "Produce ", "Meat ", "Breakfast ", "Seafood ", "Dairy ", "Frozen ", "Drinks ", "Snacks ", "Grains", "Cans & Jars ", "Spices", "Sauces & Oils", "Paper", "Cleaning", "Personal", "Baking ", "Other"]
}
解决方案
问题是由您未显示的代码引起的。您在 中返回的数字太大numberOfSections
。
推荐阅读
- javascript - 如何遍历车把中的对象数组
- javascript - 当我添加花括号以便能够向上下文添加更多元素时,如何访问 GraphqlServer 上下文对象?
- google-cloud-platform - 从 Google GCP 项目凭据 API 密钥中检索信息
- xml - 使用 xslt 2.0 将 xml 日期时间数据转换为 yyyy-MM-dd hh:mm:ss:ff
- stream - WebFlux - 如何使用 Flux
作为列表 服务方法中的参数 - android - 我无法从华为 p40 系列设备获取 Firebase 令牌
- c# - 当 TSV 文件另存为 .txt 时,如何将其呈现为 JSON?
- python - keras:无法在model.fit中将字符串转换为浮点数
- asp.net-core - 具有身份验证的 Blazor 服务器反向代理
- attributes - 如何使用 Bio.Entrez (python) 从 ncbi biosample essummary 中获取多个属性