
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to call a method after a delay in Swift(iOS)?
In this post, we will be seeing how you can delay a method call using Swift. Here we will be seeing how you can achieve the same in two ways,
So let’s get started,
We will be seeing both the example in Playground,
Step 1 − Open Xcode → New Playground.
In the first approach, we will be using asyncAfter(deadline: execute:) instance method, which Schedules a work item for execution at the specified time and returns immediately.
You can read more about it here https://developer.apple.com/documentation/dispatch/dispatchqueue/2300020-asyncafter
Step 2 − Copy the below code in Playground and run,
func functionOne() { let delayTime = DispatchTime.now() + 3.0 print("one") DispatchQueue.main.asyncAfter(deadline: delayTime, execute: { hello() }) } func hello() { print("text") } functionOne()
Here we are creating one function functionOne, where first we are declaring delay time which is 3 Seconds. Then we are using asyncAfter where we are giving the delay time as a parameter and asking to execute hello() function after 3 seconds.
If we run the above program first “one” prints then after 3 seconds “text” prints.
In the second approach, we are going to use NSTimer,
You can copy the below code in your Xcode→Single View application
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() print("one") Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(ViewController.hello), userInfo: nil, repeats: false) } @objc func hello() { print("text") } }
Run the application, you will see the same output, one is printed and after 3 seconds text is printed.
- Related Articles
- How to call a method after a delay?
- How to call a method after a delay in Android Kotlin?
- How to call a function after a delay in Kotlin?
- How to call a jQuery function after a certain delay?
- How to make phone call in iOS 10 using Swift?
- How to repeat a task repeatedly after a regular interval in Swift(iOS)?
- How to make the phone call in iOS 10 using Swift?
- How to delay a JavaScript function call using JavaScript?
- How to integrate a facebook login in swift for iOS App?
- How to create a WebView in an iOS App using Swift?
- How to answer incoming call programmatically in iOS?
- How to hide the status bar in a iOS App using Swift?
- How to call a method of a class in C#
- How to Ping External host from Swift in iOS?
- How to integrate PayU Money in iOS using swift?
