Kadio
Kadio

Reputation: 223

How to debug "Thread 1: Fatal error: Index out of range in Swift"

I have a problem with a Swift code:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.backgroundColor = UIColor.clear
    cell.detailTextLabel?.backgroundColor = UIColor.clear

    if indexPath.row % 2 == 0 {
        cell.backgroundColor = UIColor(white: 1, alpha: 0.1)
    } else {
        cell.backgroundColor = UIColor(white: 1, alpha: 0.2)
    }

    // Load feed iamge.
    let url = NSURL(string:feedImgs[indexPath.row] as! String) //** The problem - Thread 1: Fatal error: Index out of range**
    let data = NSData(contentsOf:url! as URL)
    var image = UIImage(data:data! as Data)
    image = resizeImage(image: image!, toTheSize: CGSize(width: 80, height: 80))
    let cellImageLayer: CALayer?  = cell.imageView?.layer
    cellImageLayer!.cornerRadius = 35
    cellImageLayer!.masksToBounds = true
    cell.imageView?.image = image
    cell.textLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "title") as? String
    cell.textLabel?.textColor = UIColor.white
    cell.textLabel?.numberOfLines = 0
    cell.textLabel?.lineBreakMode = .byWordWrapping

    cell.detailTextLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "pubDate") as? String
    cell.detailTextLabel?.textColor = UIColor.white

    return cell
}

How can I solve that?

Upvotes: 1

Views: 6281

Answers (2)

Abdelahad Darwish
Abdelahad Darwish

Reputation: 6067

Index out of range mean that you need to get index from array where index greater than array items

so you use index in three places check them

let url = NSURL(string:feedImgs[indexPath.row] as! String)

,

cell.textLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "title") as? String

and

cell.detailTextLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "pubDate") as? String

So to avoid this issue Make is safe with if condition

(if indexPath.row <  feedImgs.count{
  let url = NSURL(string:feedImgs[indexPath.row] as! String)
}

 (if indexPath.row <  myFeed.count){
    cell.detailTextLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "pubDate") as? String

cell.textLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "title") as? String

  }

Upvotes: 0

PPL
PPL

Reputation: 6555

You are accessing two arrays in cellForRowAt indexPath method, first is feedImgs and second is myFeed.

If both array count is same then it will not crash, if both array counts are different then it will crash for Index Out of range for one of the array.

For example, feedImgs have 4 objects in it and myFeed have 5 objects then it will crash at 4th index in feedImgs array and give you the reason Index Out of range.

Hope this helps to you.

Upvotes: 4

Related Questions