【问题标题】:Cannot form weak reference to instance of class无法形成对类实例的弱引用
【发布时间】: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.,下面有一段代码。

如果我删除 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") 
     }
}

【问题讨论】:

    标签: ios swift xcode swift3


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多