0

I am in Xcode 10.3. I try to use the provided Timer.scheduledTimer. When it comes to the selector, Xcode suggests that I should use @objc func. Is there a better way to do that? perhaps with Swift?

func startTimer() {
    countdownTimer = Timer.scheduledTimer( timeInterval: 1, 
                                           target: self, 
                                           selector: #selector(updateTime), 
                                           userInfo: nil, repeats: true)
}

@objc func updateTime() {

    //do something

}

(Here explains very well what is a selector: Timer, #selector explanation My question is if it is possible now (August 2019) to do that in a better way

Here it explains how to use Timer: How can I use Timer (formerly NSTimer) in Swift? But this question is older than 5 years. If I understand, In that moment #selector(updateTime) was the best solution. My question is if now, August 2019, there is a new simpler way without @objc )

Nrc
  • 9,577
  • 17
  • 67
  • 114
  • Look at the documentation for `Timer`. Use the `scheduledTimer` that takes a block instead of a target/selector. BTW - your code and question are about Swift, not Objective-C. – rmaddy Aug 11 '19 at 16:26
  • https://stackoverflow.com/a/40183997/1630618 – vacawama Aug 11 '19 at 16:31
  • The link I gave you in the above comment shows how to use a block and trailing closure syntax to do it the more modern Swifty way. Even though the question is old, that answer is newer. – vacawama Aug 11 '19 at 17:35

0 Answers0