0

I need a timer so I used this code:

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector:  #selector(generalKnowledge.method), userInfo: nil, repeats: true)

But I do not understand the #selector. I tried multiple times but it doesn't work.

Nae
  • 14,209
  • 7
  • 52
  • 79
Antony D
  • 75
  • 2
  • 9
  • You should generally avoid [`scheduledTimer(timeInterval:target:selector:userInfo:repeats:)`](https://developer.apple.com/documentation/foundation/timer/1412416-scheduledtimer), unless you already have an existing `@objc` method that you would like to be called by the timer. If using this function will make you write a new `@objc` method just for it, you're better off using [`scheduledTimer(withTimeInterval:repeats:block:)`](https://developer.apple.com/documentation/foundation/timer/2091889-scheduledtimer) to pass in a closure, or (equivalently), a regular Swift function or method. – Alexander Jan 22 '18 at 17:56

2 Answers2

5

selector() is where you'd add in the function that you want it to call every timeInterval you set. In your example it's every second.

Do bare in mind that in Swift 4 and above, you need to add @objc before a function if you want to call it in a selector like so:

@objc func handleEverySecond() {
    print("Hello world!")
}

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleEverySecond), userInfo: nil, repeats: true)
Daniel Dramond
  • 1,538
  • 2
  • 15
  • 26
  • It might help to point out that you can also have a parameter representing the timer in the selector. – rmaddy Jan 22 '18 at 17:29
  • But what if I want the timer to not call up a function? – Antony D Jan 22 '18 at 17:35
  • @AntonyD Explain what you want the timer to do and maybe we could help you out – Daniel Dramond Jan 22 '18 at 17:37
  • @AntonyD What would the point of the timer be if you didn't want it to call the selector? – rmaddy Jan 22 '18 at 17:50
  • @rmaddy I am making a trivia game and this timer is in the new question function that assigns the buttons the details of the the question from an array of arrays. The arrays of arrays is just an array of an array of a question. – Antony D Jan 23 '18 at 16:37
2

A selector is essentially a message that is sent to an object. It was mostly used in objective-C and Swift has tried to move away from it. However, there are still some objective-C APIs that use it, including the timer one.

This is why selectors must be marked as @objc since it needs to be exposed in order to be seen.

So when you pass a selector to the timer, you're telling it to send this message to the class when it fires.

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(action), userInfo: nil, repeats: true)

@objc func action() { print("timer fired") }

Also, it's important to remember that you need to keep a reference to the timer outside of the scope of the function.

Alexander Li
  • 781
  • 4
  • 12
  • hi, what should i do if i dont want to add @objc for a function. can i use a timer in another way? – linkliu mayuyu Oct 11 '20 at 16:46
  • @linkliumayuyu As of iOS 10, `Timer` offers a closure based variant. https://developer.apple.com/documentation/foundation/timer/2091889-scheduledtimer – Alexander Li Oct 12 '20 at 17:08