Reputation: 135
i am trying to execute a piece of code at a certain date. I can't use NSTimer as the purpose of this code si to be used in a Perfect script.`
import Foundation
let timeInterval: TimeInterval = 10
let future = Date().addingTimeInterval(timeInterval)
print("start")
while true {
if Date() > future {
print("stop")
break
}
}
the problem is that the program is ended before it had time to print "stop". Also when i try to print something inside the loop, it is printed a few time before the program ends. Any idea on how to do that?
thanks!
Upvotes: 0
Views: 49
Reputation: 59536
The while true
idea is very bad, you should use Grand Central Dispatch instead
print("start")
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
print("stop")
}
Upvotes: 1