Reputation: 73
I've stumbled across an error in my application:
fatal error: Index out of range (lldb)
I think I may have an idea as to what the problem is, however, don't have a clue on how to amend the error.
I believe due to the fact I'm using section headers, this is causing the problem. I've proof read the coding as well as trying to fix it and searching online. Below I have posted a sample of my code (didn't want to include it all as it includes a few hundred lines of code).
Essentially, I am using a TableViewController in combination with SWReveal where the user selects an option and text will appear.
class BackTableVC: UITableViewController {
struct Brands {
var sectionName : String!
var sectionBrands : [String]!
}
struct ThirdView {
var ThirdViewArray = [String]()
}
var brandArray = [Brands]()
var ThirdArray = [ThirdView]()
var brandAnswerArray = [String]()
override func viewDidLoad() {
brandArray = [
Brands(sectionName: "Bugatti", sectionBrands: ["EB 110","Veyron"])]
ThirdArray = [ThirdView(ThirdViewArray: ["EB 110","Veyron"])]
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return brandArray[section].sectionBrands.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
cell.textLabel?.text = brandArray[indexPath.section].sectionBrands[indexPath.row]
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestVC = segue.destinationViewController as! CarDetailsVC
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let ThirdAnswerArray : ThirdView
ThirdAnswerArray = ThirdArray[indexPath.row]
DestVC.brandAnswerArray = ThirdAnswerArray.ThirdViewArray
DestVC.FirstString = brandAnswerArray[indexPath.row]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return brandArray.count
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return brandArray[section].sectionName
}
}
import Foundation
struct ThirdView {
var ThirdViewArray = [String]()
}
class CarDetailsVC: UIViewController {
var FirstString = String()
var brandAnswerArray = [String]()
@IBOutlet var Label: UILabel!
override func viewDidLoad() {
Label.text = FirstString
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
_ = segue.destinationViewController as! CarDetailsVC
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
My ThirdView struct and CarDetailsVC are in separate .swift files.
The line which is giving me grief is:
DestVC.FirstString = brandAnswerArray[indexPath.row]
P.S. if I was to do this:
DestVC.FirstString = "Hello World"
Hello World is shown when selecting only the first option, then the code/application breaks an I get the same error "index out of range" on the line:
ThirdAnswerArray = ThirdArray[indexPath.row]
Upvotes: 1
Views: 247
Reputation: 2650
This simple answer is that your brandAnswerArray doesn't have enough values to give you the thing at index indexPath.row. i.e. If you have an array with 5 values and you ask it for array[8], the app will crash because index 8 doesn't exist.
Specifically, you are telling your table that you have a certain number of cells/rows:
brandArray[section].sectionBrands.count
That means for every integer, from 0, to whatever brandArray[section].sectionBrands.count is, the table is going to ask you to generate a cell. Therefore, that is the range that your indexPath.row can have.
BUT: In your prepareForSegue, you are accessing brandAnswerArray[indexPath.row], and brandAnswerArray simply doesn't have enough values to give you whatever is at that requested index (which is a risk, since you used a different portion of data to build the table).
Upvotes: 3