0

Is it possible to get a list of all the selectors that an instance of a given class responds to?

jscs
  • 63,694
  • 13
  • 151
  • 195
Orpheus Mercury
  • 1,617
  • 2
  • 15
  • 30
  • 1
    See the stackoverflow article describing [class_copyMethodList][1]. [1]: http://stackoverflow.com/questions/330030/list-selectors-for-obj-c-object – jarmod Aug 11 '13 at 19:19

2 Answers2

1

Yes. copyMethodList() returns a list of the implemented methods. There are potentially more selectors, the instance will respond to by using the forwarding mechanism.

class_copyMethodList() copies only the methods implemented at this stage of the class hierarchy. To get all you have to iterate up the superclasses:

Class class = object_getClass( instance );
while( class != Nil )
{
   // copy method list 
   class = class_getSuperclass( class );
}
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • 2
    This is not strictly the list of selectors a class responds to; a class can respond in other ways than by implementing a method with the same name -- by forwarding or by resolving a selector at runtime. – jscs Aug 11 '13 at 19:27
  • That's correct, it is only the list of methods. But there is no way to find out to which selectors a class' instance responds, if a method is not implemented. But in most cases this is not relevant. Anyway I added a clarification. – Amin Negm-Awad Aug 11 '13 at 19:35
  • Yes; as far as I know you would have to get a list of all the selectors registered with the runtime and ask the instance about every one of them -- and that list isn't public, either. – jscs Aug 11 '13 at 19:47
0

If the selectors are not hidden, you can type [instanceName (with a space after) and then hit ESC. A drop down will pop up giving you a list of visible selectors.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36