Creating a repeating timer is simple with the following code.
Repeating Timer in Swift
//Repeating timer in Swift
let timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.printMessage), userInfo: nil, repeats: true)
- timerInterval denotes the number of seconds between the firings of timer.
- target denotes the object to which send a message specified by selector.
- selector denotes the message to sent to the target after the timer gets fired.
- UserInfo can be used to send additional information about timer event in a dictionary format.
- repeats denotes whether a timer is repeating or non-repeating. If it is set to true, the timer reschedules itself after getting fired automatically until it is manually invalidated and if it is set to false, the timer fires once and gets invalidated automatically.