How to Wait in Objective-C and Swift

jylee picture jylee · Aug 8, 2011 · Viewed 82.7k times · Source

I want to change my UILabel's text after 2 seconds.

I tried setting my UILabel's text to "A text", and use sleep(2) and finally changing the text to "Another text".

But sleep(2) only freezes the app and "Another text" is set without displaying "A text" for 2 seconds.

How may I display "A text" for 2 seconds and then show "Another text"?

Answer

Yi Jiang picture Yi Jiang · Aug 8, 2013

I know I am late to this party. But I found people haven't mention thread sleep. If you are using GCD to call that function. You can use :

[NSThread sleepForTimeInterval:2.0f];   

to delay the thread for 2 seconds.

[self changeText: @"A text"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Here your non-main thread.
        [NSThread sleepForTimeInterval:2.0f];   
        dispatch_async(dispatch_get_main_queue(), ^{
            //Here you returns to main thread.
            [self changeText: @"Another text"];
        });
    });

Edit 2 (Feb 2015):

I think the NSTimer is a great solution. My solution just giving another option to achieve the goal of NSTimer.

Please read: How do I use NSTimer?

[NSTimer scheduledTimerWithTimeInterval:2.0
                                 target:self
                               selector:@selector(doSomethingWhenTimeIsUp:)
                               userInfo:nil
                                repeats:NO];

In the class, you need this method:

- (void) doSomethingWhenTimeIsUp:(NSTimer*)t {

        // YES! Do something here!!

}

Edit 3 (May 2016):

In Swift 2.0, you can use this way:

 NSTimer.scheduledTimerWithTimeInterval(2.0, 
                                         target: self, 
                                       selector: "doSomethingWhenTimeIsUp:", 
                                       userInfo: nil, 
                                        repeats: false)

It creates an NSTimer's entity and adds the timer automatically to the NSRunLoop associated with the NSThread in which the timer is created.

Edit 4 (Jun 2016):

In Swift 2.2, the way to invoke select is:

#selector(doSomethingWhenTimeIsUp(_:))

So, it is something like:

  NSTimer.scheduledTimerWithTimeInterval(2.0,
                                      target: self,
                                      selector: #selector(doSomethingWhenTimeIsUp()),
                                      userInfo: nil,
                                      repeats: false)

Edit 5 (Oct 2016):

In Swift 3, the way to invoke select is:

#selector(doSomethingWhenTimeIsUp)

So, it is something like:

        Timer.scheduledTimer(timeInterval: 2.0,
                             target: self,
                             selector:#selector(doSomethingWhenTimeIsUp),
                             userInfo: nil,
                             repeats: false)

Then, the func should looks like this:

        @objc private func doSomethingWhenTimeIsUp(){
        // Do something when time is up
        }

Edit 6 (May 2018):

In Swift 4, we can do as below way.

    let delaySeconds = 2.0
    DispatchQueue.main.asyncAfter(deadline: .now() + delaySeconds) {
        doSomethingWhenTimeIsUp()
    }  

Then, the func should looks like this:

    private func doSomethingWhenTimeIsUp(){
        // Do something when time is up
        }