0

The question (reflection or something like it?)

Is it possible (in Swift) to extract all the method signatures of an iOS protocol programmatically, such as for UITextViewDelegate, whose methods are all optional, without having to instantiate a separate class that explicitly implements all the protocol's methods?

clearlight
  • 12,255
  • 11
  • 57
  • 75
  • Instead of saying that you want to "extract all the method signatures of an iOS protocol programmatically", you'd be better off explaining exactly what you're trying to do. What I think that you want is an object that says that it responds to any protocol's selectors and forward the call to another delegate, but beyond that, I don't understand what additional work you want to perform in this delegate to justify the proxy. – zneak Jan 05 '17 at 01:08
  • AFAIK, there is almost certainly "a" way, but almost certainly not a "relatively painless" way. – zneak Jan 05 '17 at 01:08
  • I'm trying to supplement iOS with something that many believe *should* be native iOS functionality, but isn't, and trying to make it *look* and like a native iOS feature, and I'm using a 'plugin' design pattern (one of the primary uses of reflection). – clearlight Jan 05 '17 at 01:22
  • in other words, you have a delegate that implements just a few methods and forwards the rest to another delegate, and you want these methods to be automatically forwarded without manually programming them to do so? – zneak Jan 05 '17 at 01:35
  • You should look at http://stackoverflow.com/questions/4574465/objective-c-respondstoselector for a better description of the forwarding system. As it stands, and without personally testing it, I'm not convinced that you only need to implement `forwardingTarget(for:)` for your use case, especially to cover optional methods. – zneak Jan 05 '17 at 01:52
  • @zneak, @matt Where it's at so far is the extension *does* allow and call `forwardingTarget()` if prefixed with `override open dynamic func`! Yay! But it gets called for things like `keyboardAppearance()` (not in protocol) and I have to figure out what to do about that, finds nil unwrapping optional I have to figure out. May take it out of playground and build a project I can hit with the debugger. If I can get it working, I'll update the solution that prompted it and post a link here. – clearlight Jan 05 '17 at 02:26

1 Answers1

1

In this case, want to intervene as the delegate to intercept one of the methods and do some related operation to that activity, then daisy chain the delegate call forward. But unfortunately, becoming the delegate entails responsibility to forward all of the protocol the downstream consumer

If you're saying what I think you're saying, there actually is a very simple way to do this: implement forwardingTarget(for:), as I do here:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch12p611tabBarMore/ch25p882tabBarMore/MyDataSource.swift

The idea is that I don't know what the table view's dataSource actually does, because the table view and its data source belong to Cocoa, but I want to act as a "man in the middle" between them and just tweak the behavior of one data source method if it is called. All other method calls just get passed along, without my even knowing what they are.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • ************************** B R I L L I A N T !!!!!!!!! ******************** I thought it was almost pointless to ask such an arcane idiopathic question. That is invaluable. Thanks immensely. This has been bugging me for awhile. – clearlight Jan 05 '17 at 01:29
  • Cocoa Foundation is full of cool stuff like that. Let's hope they don't throw the baby out with the bathwater as we migrate more and more towards Swift... – matt Jan 05 '17 at 01:30
  • How does that interact with `respondsToSelector`? – zneak Jan 05 '17 at 01:31
  • Not sure what you're asking; you can see me calling it. – matt Jan 05 '17 at 01:31
  • @matt, if I call `respondsToSelector` on an object that implements `forwardingTargetFor`, do I get a positive result if the forwarding target responds to that selector? This is important because Apple [recommends](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html) calling `respondsToSelector` to determine if an object implements an optional method. – zneak Jan 05 '17 at 01:34
  • Try it and see. If not, you can always override `respondsToSelector` to give the answer you want to give. However, my code doesn't do that and I have not crippled the table view, so my guess is that the answer is yes. – matt Jan 05 '17 at 01:36
  • I don't have access to an Objective-C or Swift compiler here. It being your answer, I find that the next best person to verify that it works would be you. I'm absolutely willing to believe that it works until then (it wouldn't be a hard thing to implement, after all). – zneak Jan 05 '17 at 01:38
  • 1
    Knowing me, I wouldn't trust me any further than I can throw me, which is not far. – matt Jan 05 '17 at 01:41
  • One should never throw oneself, unless one is exceptional. (don't hit me) – clearlight Jan 05 '17 at 01:43
  • 1
    Didn't catch that. (ditto) – matt Jan 05 '17 at 01:45
  • 1
    You can always try again. – Andy Ibanez Jan 05 '17 at 03:15