【发布时间】:2017-12-17 12:26:40
【问题描述】:
我收到了错误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.,下面有一段代码。
如果我删除 addObserver 和 removeObserver 行,我的代码不会崩溃。我不确定为什么会这样。有没有人有任何想法?提前感谢您的帮助!
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")
}
}
【问题讨论】: