Reputation:
My app uses multiple threads of NSTimer
Object.
Within the one function (called randomly at random times, not a fixed delay) I want it to pause the whole app, pause the threads for 1 second
only. I have the following code:
[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];
Which is objective C, and I tried translating it into Swift like this:
self.performSelector(Selector:changeColourOfPage(), withObject: self, afterDelay: 1.0)
But I am getting the error Missing argument for parameter 'waitUntilDone' in call
and when I put that in, it says it wants the argument modes
but when I put that in it says Extra argument modes
.
I cannot figure out how to pause the app and all it's threads for a couple of seconds, and then carry on like normal?
Any ideas?
Upvotes: 8
Views: 7520
Reputation: 599
Another way in Swift 3.1
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
changeColourOfPage()
}
You can substitute other units like .milliseconds(1000)
or .microseconds(1_000_000)
or .nanoseconds(1_000_000_000)
for the time interval.
Upvotes: 3
Reputation: 7537
Sorry to answer an old question, but just came along a better way to do this -
import Darwin //Put this line at the beginning of your file
func pauseForAMoment() {
sleep(1)
changeColorOfPage()
}
Upvotes: 1
Reputation: 64644
The performSelector methods aren't available in Swift. You can get the delay functionality by using dispatch_after.
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC * 1))
dispatch_after(delayTime, dispatch_get_main_queue()){
changeColourOfPage()
}
Upvotes: 11