gpt4 book ai didi

swift - NSInternalInconsistencyException 观察者 contentSize

转载 作者:行者123 更新时间:2023-11-30 12:16:29 27 4
gpt4 key购买 nike

收到此错误

2017-07-28 20:17:34.636 App[78013:1799389] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7fa97b0bf400 of class App.MessagesCellTextView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x618000434e80> (
<NSKeyValueObservance 0x618000243c60: Observer: 0x7fa97b0bf400, Key path: contentSize, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x618000243c90>

当我离开聊天 Controller 并返回时。

这是我的 contentSize 代码

var observerAdded: Bool = false

override func awakeFromNib() {
super.awakeFromNib()

self.addObserver(self, forKeyPath:"contentSize", options:.new, context:nil)
observerAdded = true



}

deinit {
if (observerAdded) {
observerAdded = false
self.removeObserver(self, forKeyPath: "contentSize")
}
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let textView = object as? UITextView {

var y: CGFloat = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2.0
y = (y < 0.0 ? 0.0 : y)

textView.contentOffset = CGPoint(x: 0, y: -y)

}
}

奇怪的是,我尝试删除所有这些代码,但我仍然收到相同的错误,什么!!。

最佳答案

如果观察者未添加到您的 awakeFromnib 方法中,请检查该标志,然后相应地添加它:-

override func awakeFromNib() {
super.awakeFromNib()
if !observerAdded {
self.addObserver(self, forKeyPath:"contentSize", options:.new, context:nil)
observerAdded = true
}
}

关于swift - NSInternalInconsistencyException 观察者 contentSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376528/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com