Could you please help, how to use Timer instance in Swift 4 on Linux Ubuntu 16.04?
When I try to do:
let timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(MyClass.myMethod), userInfo: nil, repeats: true)
I got error: error: '#selector' can only be used with the Objective-C runtime
You can use the block-based timer functions on Linux. Here is a minimal self-contained example which compiles and runs both in Xcode 9.1 and on 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()
(I added the exit(0)
only because the IBM Swift Sandbox limits the
program execution time to 5 seconds.)
Alternatively, use a DispatchSourceTimer
as demonstrated
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With