How to solve 'Thread 1: Fatal error: Index out of range' in a two dimensional array
How to solve 'Thread 1: Fatal error: Index out of range' in a two dimensional array
我在启动我的应用程序时发生崩溃 'Thread 1: Fatal error: Index out of range'。完整错误:
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
.
中返回的数字太大
我在启动我的应用程序时发生崩溃 'Thread 1: Fatal error: Index out of range'。完整错误:
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
.