你能帮忙,如何在Linux Ubuntu 16.04上的Swift 4中使用Timer实例?
当我尝试做的时候:
let timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(MyClass.myMethod), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
我收到错误:错误:'#selector'只能与Objective-C运行时一起使用
您可以在Linux上使用基于块的计时器功能.这是一个最小的自包含示例,它在Xcode 9.1和https://swift.sandbox.bluemix.net/#/repl中编译和运行:
import Foundation
import CoreFoundation
let timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { timer in
print("In timer function")
exit(0)
}
CFRunLoopRun()
Run Code Online (Sandbox Code Playgroud)
(我exit(0)之所以添加,只是因为IBM Swift Sandbox将程序执行时间限制为5秒.)
或者,使用DispatchSourceTimer这表现
在这里.
| 归档时间: |
|
| 查看次数: |
603 次 |
| 最近记录: |