0

I am implementing the scheduleTimer function of a Timer in a mock Timer to be able to unit test my code. It looks like this:

static func scheduledTimer(
    timeInterval ti: TimeInterval,
    target aTarget: Any,
    selector aSelector: Selector,
    userInfo: Any?,
    repeats yesOrNo: Bool) -> Timer

I need to be able to call aSelector like a closure, but I'm not sure how that's possible with it being a Selector. How can I do this?

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
  • It’s an Objective C mechanism and it’s easy to do in Objective C. – matt Mar 14 '19 at 12:57
  • @matt any way to do it in swift? – Tometoyou Mar 14 '19 at 12:58
  • 1
    If `aTarget` is not subclass `NSObject` you can't do nothing about it, otherwise you can cast it to `NSObject` and `performSelector…` as usual. – user28434'mstep Mar 14 '19 at 13:00
  • Possible duplicate of [Objective-C: Calling selectors with multiple arguments](https://stackoverflow.com/questions/1018195/objective-c-calling-selectors-with-multiple-arguments) – delavega66 Mar 14 '19 at 13:08
  • By the way, it does not make much sense to mock timer. You can just call the method directly with the same result. – Sulthan Mar 14 '19 at 13:10
  • You can't. [reference](https://developer.apple.com/documentation/swift/using_objective-c_runtime_features_in_swift) As this reference said, Selector refers Object-C method's names. But I couldn't get a thing. Why you need to use closure instead of creating a method in your test class and send this method to your selector? – Ekrem Duvarbasi Mar 14 '19 at 13:59

0 Answers0