0

I'm gettiing a fatal error and i don't know why. i have tried changing the '!' with '?' at _title but it still don't work.

class Post: NSObject, NSCoding {
   
    private var _img: String!
    private var _title: String!
    private var _descLbl: String!
    
    var img: String {
        return _img
    }
    
    var titleLbl: String {
        return _title
    }
    
    var descLbl: String {
        return _descLbl
    }
    
    init(imagePath: String, title: String, description: String) {
        self._img = imagePath
        self._title = title
        self._descLbl = description
    }
}

Here is where i get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

class PostCell: UITableViewCell {

    @IBOutlet weak var postImg: UIImageView!
    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var descLbl: UILabel!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func configureCell(post: Post) {
        title.text = post.titleLbl //Here i get the error
        descLbl.text = post.descLbl
    }
}

Here i call the function configureCell in ViewController:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let post = posts[indexPath.row]
    
    if let cell = tableView.dequeueReusableCellWithIdentifier("postCell") as? PostCell {
        cell.configureCell(post)
        return cell
    }else{
        var cell = PostCell()
        cell.configureCell(post)
        return cell
    }
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
David Dume
  • 27
  • 6
  • The `Post` class does not compile: `error: type 'Post' does not conform to protocol 'NSCoding'` – Luca Angeletti Feb 14 '16 at 10:41
  • 1
    What's the purpose of those backing private variables in the `Post` class? If you want read-only properties just declare them as constants (`let`) – vadian Feb 14 '16 at 10:43
  • Is the class of the custom cell set to `PostCell` in Interface Builder? – vadian Feb 14 '16 at 10:50
  • 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) – pkamb Oct 12 '21 at 00:57

1 Answers1

0

The issue is most possibly that your @IBOutlet:s are missing proper connections. You reach the runtime exception, however, only when you try to access these "free-floating" disconnected outlet properties for the first time; namely in the .configureCell(...) call in tableView(..).

E.g., the following scaled-down example yields the same error for "non-connected" @IBOutlet:s

class PostCell: UITableViewCell {

        // non-connected outlets
        @IBOutlet weak var postImg: UIImageView!
        @IBOutlet weak var title: UILabel!
        @IBOutlet weak var descLbl: UILabel!

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

        func configureCell() {
            title.text = "foo" // <--
            descLbl.text = "bar"
        }
}

// ... elsewhere
var cell = PostCell()    
cell.configureCell() 
    // fatal error: unexpectedly found nil while unwrapping an Optional value

Make sure that, e.g. in the Connections Inspector in the Interface Builder, that all these three outlets are properly connected to the intended UI elements. For details, see e.g. the following thread

Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • Thanks for your answer dfri. Everything is connected the right way, so what's the problem? – David Dume Feb 14 '16 at 14:39
  • @DavidDume Have you double checked vadian:s note above; that your custom cells are set to `PostCell` in the attributes inspector in the Interface Builder? – dfrib Feb 14 '16 at 15:17
  • I fixed it. I didn't put the identifier at the cell in the storyBoard so this is why this code "tableView.dequeueReusableCellWithIdentifier("postCell")" didn't work. Thanks for your help :) – David Dume Feb 15 '16 at 15:22
  • @Dume I see, a common mistake, good to hear you worked it out :) – dfrib Feb 15 '16 at 18:42