0

Is there any way to return an @objc function in swift?

Currently I have this

private func getFunction(for index: IndexPath) -> () -> () {
        return {
            print("pressed")
        }
}

I am trying to use that as a selector like so

UITapGestureRecognizer(target: self, action: #selector(self.getFunction(for: index)))

But #selector only works with ObjC function, so I would need to do something like this

return @objc {
    print("pressed")
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
zoecarver
  • 5,523
  • 2
  • 26
  • 56
  • The value inside `#selector()` needs to be a compile-time reference to a function or property. You can't call a function at runtime and use its result as the argument to `#selector()`. – rmaddy Mar 31 '18 at 16:42
  • And the selector you pass to a gesture recognizer must have either no parameters or one parameter and that one parameter must be the gesture recognizer. – rmaddy Mar 31 '18 at 16:48
  • @rmaddy thanks for the information. Because of the closure, the function would be called with no parameters. See what I have is return value of the function: `self.getFunction(for: index)` not `self.getFunction(for: index)()`. Anyway, thanks for the answer. – zoecarver Mar 31 '18 at 16:50

0 Answers0