创建计时器时,只有以下选项:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds invocation:(NSInvocation *)invocation repeats:(BOOL)repeats;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats;
如您所见,没有工厂方法可以创建一个带有 block 作为其执行 Action 的计时器。
为什么是这样?还是我错过了什么?
最佳答案
NSTimer 在 block 之前并且尚未更新以使用它们。如果您想使用 block (正确),请使用 GCD 调度源,即 dispatch_source_set_timer
及相关功能。
在 ARC 下,请注意保留周期(尽管您也必须注意那些使用 NSTimer 的循环)。
关于ios - 为什么 NSTimer 触发的 Action 不能由 block 指定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813810/