使用 iOS-Charts 2.2.3 离开 Charts VC 时未删除观察者?

Observer not removed when leaving Charts VC with iOS-Charts 2.2.3?

我正在努力向我的应用程序添加一些图表,并且有一个带有 iOS-Charts 2.1.4 的工作版本。更新到 iOS-Charts 2.2.3 后,从包含我的 CombinedChartView 的视图控制器返回时出现以下错误:

*** Terminating app due to uncaught exception     'NSInternalInconsistencyException', reason: 'An instance 0x168c5a00 of class Charts.CombinedChartView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x171366c0> (
<NSKeyValueObservance 0x15d0dda0: Observer: 0x168c5a00, Key path: bounds, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x15d1b090>
<NSKeyValueObservance 0x15d17a10: Observer: 0x168c5a00, Key path: frame, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x15db49b0>)'
*** First throw call stack:
(0x217b62eb 0x20f82dff 0x217b6231 0x21f60095 0x20f9d3cd 0x216c9921 0x217774c7 0x216c9bb9 0x216c99ad 0x22943af9 0x259b5fb5 0xf7ed1 0x2137c873)
libc++abi.dylib: terminating with uncaught exception of type NSException

注意:我并不是自愿在此 VC 中注册任何观察员。

有人看到最新版本的 iOS-Charts 相同吗?顺便说一句,很棒的图形库!

知道我做错了什么吗?

我不确定你的代码有什么问题,但你的库好像坏了。

iOS-图表确实删除了 deinit() 中的观察者:

deinit
{
    self.removeObserver(self, forKeyPath: "bounds")
    self.removeObserver(self, forKeyPath: "frame")
}

然而你的痕迹是

Charts.CombinedChartView was deallocated while key value observers were still registered with it

这是一个悖论。你可能想检查你的图书馆。当组合图表被取消时,观察者应该被移除。您还可以在 deinit 中添加断点以进行调试。

感谢@Wingzero 的建议,我找到了解决方案!

所以我的问题是 ChartViewBase.init() 方法被调用了两次,因此观察者注册了两次,但 deinit() 只被调用了一次,所以有一组重复的观察者悬而未决。

两次 init() 调用的原因是我开始使用 v.2.1 上的 iOS-Charts 库,当时需要在我的 VC 中手动初始化 CombinedChartView的 viewDidLoad() (或者我认为)。随着 iOS-Charts 2.2.3(或 Swift 2?)的更新,这不再需要,并且会导致双重初始化,因此 ChartViews 的一个实例被挂起。

希望这可以帮助其他遵循相同的 iOS-Charts 2.1 -> 2.2.3 更新路径的人。我不得不说,我强烈推荐 iOS-Charts 作为 iOS 的图形库。这只是我的错误。

另请注意,cocoaPod 还不知道 2.2 版本的库。