-1

I'm getting this error:

Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I know it means that I forced unwrapped an optional. But the problem is that it happens in this line of code:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell  //This is the problem
            let object = self.fetchedResultsController.object(at: indexPath)
            cell.nameLabel.text = object.title
            if let imgURL = object.imageURL {
                let realURL = NSURL(string: imgURL)
                let data = NSData(contentsOf: (realURL as URL?)!)
                cell.imgView.image = UIImage(data: data! as Data)
            }

            return cell
        }

So my guess was that I made a mistake when I assigned the class to the Cell or the identifier, but I have double checked and it's the correct. I'm not sure what else can be the problem. I have to say, I made the code in another project template (it's a blog reader feature) to test it first and then I copied the code (It was working), so I'm not sure if it can be the origin of the problem.

Any ideas of what else can I do?

*This is the code inside the TableViewCell:

import UIKit
import CoreData

class TableViewCell: UITableViewCell {

    @IBOutlet weak var imgView: UIImageView!

    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Enrique
  • 293
  • 1
  • 2
  • 15
  • Do you have a crash log? It could pint point where lies your issue. – Larme Aug 24 '17 at 14:49
  • Yes, it says: `fatal error: unexpectedly found nil while unwrapping an Optional value` – Enrique Aug 24 '17 at 14:53
  • 1
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Dávid Pásztor Aug 24 '17 at 14:55

1 Answers1

1

Try to use this line to dequeue the cell:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
Tung Fam
  • 7,899
  • 4
  • 56
  • 63