Outlet is connected, but Unexpectedly found nil while unwrapping an Optional value

I'm trying to set up a UITableView inside a UITableViewCell, and I followed the selected answer to Is it possible to add UITableView within a UITableViewCell

This is my code for the TableViewCell class which holds the inner TableView:

import UIKit

class tvc_item: UITableViewCell, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var ivItem: UIImageView!
    @IBOutlet weak var lblItemName: UILabel!
    @IBOutlet weak var tvInner: UITableView!

    var itemdata = Item()

    override func awakeFromNib() {
        super.awakeFromNib()
        setUpTable()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpTable()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        //tvInner?.frame = CGRectMake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func setUpTable(){
        tvInner.delegate = self // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
        tvInner.dataSource = self
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemdata.rows.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_itemrow") as! cell_itemrow
        cell.lblRowLabel.text = String(itemdata.rows[indexPath.row].size)
        return cell
    }
}

When I run the code it dies on this line:

tvInner.delegate = self

with error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

So I assume tvInner is nil, but why? And what can I do about it?

tvInner's outlet seems to be connected correctly and shows a dark circle inside the outlet icon.

You need to override awakeFromNib and move setUpTable() inside this method.

Outlet is connected, but Unexpectedly found nil while unwrapping , setUpTable() } func setUpTable(){ tvInner.delegate = self // Thread 1: Fatal error​: Unexpectedly found nil while unwrapping an Optional value  check your outlets, I've had this before labels and buttons can just become dis connected (if for instance you have adjusted constraints ect) a simple re connection can sometimes fix the issue That means your storyboard doesn't have a link to your IBOUTLET, please verify

Fix for «Unexpectedly found nil while unwrapping an Optional value», but don't unwrap them safely and you'll get the «Found nil while unwrapping an Understanding The «Unexpectedly found nil while unwrapping an Optional value» Error When a view controller hasn't been loaded yet, the outlet for Working With Codable And JSON In Swift · The Scene Delegate In  Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. So I assume tvInner is nil, but why? And what can I do about it? tvInner's outlet seems to be connected correctly and shows a dark circle inside the outlet icon.

It seems you forgot to put setupTable() method on override init.

Unexpectedly found nil while unwrapping an Optional value, Unexpectedly found nil while unwrapping an Optional value. [deleted]. Share11. 0. 11 Comments sorted by Your label outlet is probably not connected in IB. 4  fatal error: unexpectedly found nil while unwrapping an Optional value. Have you seen this yet working with Swift yet? If so, you’re not alone. In this post I’m going to dig in to what this means, and explain a bit about optionals in Swift.

Label.text Error!, Unexpectedly found nil while implicitly unwrapping an Optional value: file. I have this class where I already have an outlet for the label. If the outlet is nil in the view controller, then you’re calling that function before the controller’s view stack has loaded. You could try print (self.view) from the controller before you try to read the text field’s text. But I would just consider when you’re calling the function that interacts with the text field.

IBOutlet is nil, but it is connected in storybo |Apple Developer Forums, IBOutlet is nil, but it is connected in storyboard - fatal error: unexpectedly found nil while unwrapping an Optional value. 5496 Views 2 Replies. How To Fix Swift Fatal Error: Unexpectedly Found Nil While Unwrapping An Optional Value Jerry Zhao July 12, 2019 0 Swift optional variable is a variable which can has nil ( same to null ) value.

Crashing with Swift Optionals – Swift Dev Journal, Fatal error: Unexpectedly found nil while unwrapping an Optional value But if you forget to connect the outlet, the outlet is nil and your app  In Swift, Optional is a generic type that can contain a value (of any kind), or no value at all. In many other programming languages, a particular "sentinel" value is often used to indicate a lack of a value. In Objective-C, for example, nil (the null pointer) indicates the lack of an object.

Comments
  • I added that now (and I edited my question to show the current code) but it still gives the same error.
  • @Gimmethe411 Remove setUpTable call from the init
  • I tried that but it says 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'
  • Remove all overrided init functions!
  • Compiler says required initializer because you have one overrided init. Remove overrides.