@selector() in Swift?
Asked 07 September, 2021
Viewed 2.9K times
  • 55
Votes

I'm trying to create an NSTimer in Swift but I'm having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), userInfo: nil, repeats: true)

test() is a function in the same class.


I get an error in the editor:


  

Could not find an overload for 'init' that accepts the supplied arguments

When I change selector: test() to selector: nil the error disappears.

I've tried:

  • selector: test()
  • selector: test
  • selector: Selector(test())

But nothing works and I can't find a solution in the references.

23 Answer