UISwitch deinit 从未调用过

UISwitch deinit never called

几天前我发布了一个问题 但我仍然没有找到答案。我认为这是一个错误,希望将来能修复它,但不禁认为这也可能是我自己做错的事情。

有了这个问题,我希望能够确认这是一个错误,这样我就可以继续从事我的项目,而不必太担心它。如果此问题重复,我会立即删除问题。

我已经将 UISwitch 添加了 deinit 的子类,这样我就可以看到它何时被释放。

class CustomSwitch: UISwitch { deinit {print("Switch has been deinitialized")} }

然后我在我的视图控制器中添加了一个 属性:

var customSwitch: CustomSwitch? = CustomSwitch()

为了测试它,我添加了一个具有以下功能的按钮:

func removeSwitch() {
    print(customSwitch)
    customSwitch = nil
    print(customSwitch)
}

控制台打印出以下内容:

Optional(<Debugging.CustomSwitch: 0x7f9ec91083c0; baseClass = UISwitch; frame = (0 0; 51 31); layer = <CALayer: 0x6100000384a0>>)
nil

未打印 deinit 消息,在仪器中它显示 customSwitch 仍在内存中。

我唯一能找到的是 this

根据Apple Docs,我认为我们不能手动取消初始化。它说 Deinitializers are called automatically, just before instance deallocation takes place. You are not allowed to call a deinitializer yourself.

这(最终)在 iOS 10.2

中得到修复