ios - swift "timer"变量

标签 ios swift timer thread-safety

我需要 Swift 中的多线程安全 Int 变量,它可以在不同的线程中递增或递减。此外,此变量必须每秒递减 1,并在其值为零时通知(通过 block 或选择器)。

实现它的最佳方法是什么?

最佳答案

关于apple swift block你不用考虑。

One of the primary reasons to choose value types over reference types is the ability to more easily reason about your code. If you always get a unique, copied instance, you can trust that no other part of your app is changing the data under the covers. This is especially helpful in multi-threaded environments where a different thread could alter your data out from under you. This can create nasty bugs that are extremely hard to debug.

https://developer.apple.com/swift/blog/?id=10

Int is a value type .因此,您可以放心地在多个线程中使用它。

关于ios - swift "timer"变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48403960/

相关文章:

ios - 单元格宽度对于表格 View 宽度来说太大

ios - 在 Ios 中合并两个视频

mysql - 动态 MySql 查询每天返回 X 次结果

ios - 从 PopOver 更改标签的文本

ios - 像 Pebble 一样捕获所有 iOS 推送通知

ios - 解析 JSON Swift 下标错误

java - 显示倒计时

c# - 相当于 30 分钟的计时器滴答声是多少?

ios - 从 UIView 子类推送导航 Controller 会导致崩溃

ios - 林飞IOS : SSL handshake failed : X509 - Certificate verification failed