Cannot form weak reference to instance of class

Viewed 3248

I'm getting the error Cannot form weak reference to instance (0x15919e00) of class TestClass. It is possible that this object was over-released, or is in the process of deallocation. with the bit of code below.

If I remove the addObserver and removeObserver lines, my code doesn't crash. I'm not really sure why that's happening though. Does anyone have any ideas? Thanks in advance for the help!

class TestClass: NSObject {

     lazy var tableView: UITableView = {

         let tableView = UITableView(frame: .zero, style: .plain)
         tableView.rowHeight = UITableViewAutomaticDimension
         tableView.estimatedRowHeight = 52.0
         tableView.delegate = self
         tableView.dataSource = self

         let nib = UINib(nibName: "CustomCell", bundle: nil)
         tableView.register(nib, forCellReuseIdentifier: "customCell")

         tableView.addObserver(self, forKeyPath: "myPath", options:nil, context: nil)

         return tableView
     }()

     deinit {
         tableView.removeObserver(self, forKeyPath: "myPath") 
     }
}
1 Answers
Related