iOS中self.timer = nil与[self.timer invalidate]之间的区别是什么? [英] What is difference between self.timer = nil vs [self.timer invalidate] in iOS?

查看:287
本文介绍了iOS中self.timer = nil与[self.timer invalidate]之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释我self.timer=nil vs [self.timer invalidate]吗?

Can anyone explain me self.timer=nil vs [self.timer invalidate]?

self.timer的内存位置到底发生了什么?

What exactly happens at the memory location of self.timer?

以我的代码

self.timer=nil

不会停止计时器,但是

[self.timer invalidate]

停止计时器.

如果您需要我的代码,我也会进行更新.

If you require my code I will update that too.

推荐答案

一旦您不需要运行计时器,请使计时器对象无效,然后再取消其引用.

Once you have no need to run timer, invalidate timer object, after that no need to nullify its reference.

这是Apple文档所说的: NSTimer

This is what Apple documentation says: NSTimer

一旦在运行循环中进行了安排,计时器就会在指定的时间触发 直到它失效的时间间隔.非重复计时器会使该计时器无效 发射后立即自身.但是,对于重复计时器,您 必须通过调用其使自己无效的计时器对象 方法.调用此方法要求从计时器中删除计时器 当前运行循环;因此,您应该始终调用无效者 方法与安装计时器的线程相同. 使计时器失效会使它立即禁用,从而使其不再 影响运行循环.然后,运行循环将删除计时器(以及 它对计时器有很强的参考性),或者就在 invalidate方法返回或在以后某个时间返回.一旦失效, 计时器对象无法重用.

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

这篇关于iOS中self.timer = nil与[self.timer invalidate]之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆