I'm getting a crash when launching my app 'Thread 1: Fatal error: Index out of range'. Full error:
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)
Could anyone give me directions in how I can solve this error? I'm getting the error on this line:
return twoDArray[section].items.count
This is my full ViewController file:
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
}
And here is the FoodData struct:
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"]
}
The problem is caused by code you didn’t show. You are returning a too big number in numberOfSections
.