分配 NSTimer 时不调用 Deinit

Deinit is not called when assign NSTimer

这就是我在 UIViewController 的子类中声明 属性 的方式:

private weak var timer: NSTimer?

这就是我在 viewDidLoad() 中所做的:

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateTimer", userInfo: nil, repeats: true)

这是我的 deinit:

deinit {
    timer?.invalidate()
    timer = nil
}

由于 NSTimer,未调用 Deinit。不要开玩笑说 NSTimer 里面有一个对我的控制器的强烈引用:) 我该如何解决这个问题?

我认为@i_am_jorf给了你一个很好的方向。如果您查看 the documentation 的 invalidate 方法,您会找到完整的解释为什么它会那样做

This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer, either just before the invalidate method returns or at some later point.

If it was configured with target and user info objects, the receiver removes its strong references to those objects as well.

所以是的,如果没有在适当的时候调用 invalidate,就会创建保留循环