OGeek|极客世界-中国程序员成长平台

标题: ios - 无法形成对类实例的弱引用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:16
标题: ios - 无法形成对类实例的弱引用

我收到错误 无法形成对 TestClass 类的实例 (0x15919e00) 的弱引用。可能是这个对象被过度释放,或者正在被释放。下面的代码。

如果我删除 addObserverremoveObserver 行,我的代码不会崩溃。我不确定为什么会这样。有没有人有任何想法?提前感谢您的帮助!

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") 
     }
}



Best Answer-推荐答案


惰性变量和弱引用并没有真正融合在一起。我认为问题在于被声明为惰性变量的 tableView。将其更改为只读变量,您应该没问题...

关于ios - 无法形成对类实例的弱引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45068469/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4