iOS中self.timer = nil vs [self.timer invalidate]之间的差异是什么? [英] What is diffrence between self.timer = nil vs [self.timer invalidate] in iOS?

查看:85
本文介绍了iOS中self.timer = nil vs [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
方法自行使计时器对象无效。调用此方法请求从
当前运行循环中删除计时器;因此,您应始终从安装计时器的同一线程中调用invalidate
方法。
使计时器失效立即禁用它,使其不再是
影响运行循环。运行循环然后在
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 vs [self.timer invalidate]之间的差异是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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