In the code below I initialized the Images array with a list of image file names. I want the order of the images to be aligned with the order of the Names array. This is what I attempted and I got an error saying
Thread 1: EXC_BAD_INSTRUCTION ( code=EXC_I386_INVOP, subcode = 0x0)The console output: fatal error: Array index out of range (lldb)
Code
class NonameTableViewController: UITableViewController {
var Names = [" Ferro", "Korean", "HUH?","CatCafe", "UINITY", "FAKESTORE" ,"IRANOUTOFNAMES", "OKAY", "KEEP CODING"]
var Images = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafelois1.jpg"," petiteoyster.jpg", "forkeerestaurant.jpg"]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIndentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIndentifier, forIndexPath: indexPath)
//configure cell
cell.textLabel?.text = Names[indexPath.row]
cell.imageView?.image = UIImage(named: Images[indexPath.row])
return cell
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return Names.count
}
Namesarray has a higher count thanImagesI guess you are using the former to define thenumberOfRowsInSectionwhen trying to pick out an element fromImagesat an index outside the array's range it will crash like you describe... If this is not the case could you please show us yournumberOfRowsInSectionmethod?