ios - 为什么 NSTimer 触发的 Action 不能由 block 指定?

标签 ios selector objective-c-blocks nstimer nsinvocation

创建计时器时,只有以下选项:

+ (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/

相关文章:

objective-c - 使用 void 而不是 IBAction 作为方法的返回类型

ios - 如何使用自动布局在 subview 中创建 UIGestureRecognizer?

xml - 在xpath中挖出电子邮件地址

javascript - 这个选择器的功能

objective-c - 管理调用委托(delegate)回调 block 的对象的内存

ios - 如何从 xcode 4.2 中的 nsstring 或 byte 获取 JSON 数组数据?

ios - 在 TableView 中根据文本标签加载不同的图像

php - 如何简化我的国家选择菜单 PHP/mysql

ios - xcode 保留周期警告仅有时发生

没有完成 block 的 iOS 同步动画