预定的定时器会自动释放吗
does a scheduled timer automatically get deallocated
我正在使用以下 NSTimer class 方法:
NSTimer.scheduledTimerWithTimeInterval(
3,
target: self,
selector: Selector("setWaitThreeSecondsFalse"),
userInfo: nil,
repeats: false
)
返回的计时器在选择器运行后是否被释放?还是我必须明确地使计时器无效?
来自 Apple 文档:
Timers work in conjunction with run loops. To use a timer effectively,
you should be aware of how run loops operate—see NSRunLoop and
Threading Programming Guide. Note in particular that run loops
maintain strong references to their timers, so you don’t have to
maintain your own strong reference to a timer after you have added it
to a run loop.
由于 运行 循环持有对 NSTimer
对象的字符串引用,您必须使 NSTimer
无效才能将其从 运行 循环中删除。
编辑
这显然对重复计时器有效。正如 rmaddy 指出的那样,非重复会自动失效!
像您这样的非重复计时器一旦触发就会自行失效。所以在这种情况下你不需要做任何事情,除非你想在它触发之前使它无效。
来自 NSTimer
的文档:
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.
只要你没有对定时器的强引用,它就会在失效后被释放。
我正在使用以下 NSTimer class 方法:
NSTimer.scheduledTimerWithTimeInterval(
3,
target: self,
selector: Selector("setWaitThreeSecondsFalse"),
userInfo: nil,
repeats: false
)
返回的计时器在选择器运行后是否被释放?还是我必须明确地使计时器无效?
来自 Apple 文档:
Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.
由于 运行 循环持有对 NSTimer
对象的字符串引用,您必须使 NSTimer
无效才能将其从 运行 循环中删除。
编辑 这显然对重复计时器有效。正如 rmaddy 指出的那样,非重复会自动失效!
像您这样的非重复计时器一旦触发就会自行失效。所以在这种情况下你不需要做任何事情,除非你想在它触发之前使它无效。
来自 NSTimer
的文档:
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.
只要你没有对定时器的强引用,它就会在失效后被释放。