16

The only difference I observed on my own is that respondsToSelector's receiver can either be a class or an instance, while instancesRespondToSelector can only have a class receiver. What else makes them different, though? Are there any performance issues with one or the other?

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
  • respondsToSelector's receiver can only be an instance object. instancesRespondToSelector's receiver can only be a Class object. The + or - does make a difference in the type of method – Karthick Oct 18 '14 at 06:07

3 Answers3

26

Under the hood, -[NSObject respondsToSelector:] is implemented like this:

- (BOOL)respondsToSelector:(SEL)aSelector {
    return class_respondsToSelector([self class], aSelector);
}

and +[Class instancesRespondToSelector:] is implemented like this:

+ (BOOL)instancesRespondToSelector:(SEL)aSelector {
    return class_respondsToSelector(self, aSelector);
}

(I used Hopper on CoreFoundation to figure this out.)

So, there's basically no difference. However, you can override respondsToSelector: in your own class to return YES or NO on a per-instance basis (NSProxy does this). You can't do that with instancesRespondToSelector:.

B.S.
  • 21,660
  • 14
  • 87
  • 109
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 2
    Little remark, `instancesRespondToSelector:` **can** (and **must**, if you implement message forwarding mechanism) be override. See [Objective-C Runtime Programming Guide, section Forwarding and Inheritance](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html) – Lexandr Jul 22 '13 at 18:04
  • @rob_mayoff I totally know what you are saying here, but just a small point: in your first statement you have [NSObject respondsToSelector:], but NSObject is itself a class name so it confused for a bit. You meant NSObjectInstance right? – pnizzle Feb 24 '15 at 07:29
  • It is common to write `-[NSObject respondsToSelector:]` when referring to an instance message defined by the `NSObject` class (that is, a message that can be sent to any instance of `NSObject` or its subclasses). We write `+[NSObject instancesRespondToSelector:]` to refer to a class message defined by the `NSObject` class. – rob mayoff Feb 24 '15 at 07:40
9

One difference is respondsToSelector can't tell you if an instance inherits a method from its superclass, so if you want to do something like [super respondsToSelector:_cmd]; it wont work, you need to to [[self superclass] instancesRespondToSelector:_cmd];

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • Hm, okay. But isn't that more because of Objective-C's inheritance semantics, not really a difference between the two methods? Because if I'm not mistaken, by your example, `[[self superclass] respondsToSelector:_cmd];` would work. – Matthew Quiros Jul 20 '12 at 07:42
  • no, because there is no method on Class called respondsToSelector:. respondsToSelector is part of the NSObject protocol, which Class doesnt conform to – wattson12 Jul 20 '12 at 08:26
  • @wattson12 `NSObject` and its subclasses respond to the class method `+respondsToSelector:`. – rob mayoff Jul 20 '12 at 20:24
  • is that actually a class method or is it responding to its own instance method? i couldnt find a +respondsToSelector: in the docs? – wattson12 Jul 21 '12 at 09:49
4

respondsToSelector: is an instance method and determines whether an object, which could be an instance of a class or a class object, responds to a selector. When you pass a instance you are testing for an instance method, when you pass a class object you are testing for a class method.

instancesRespondToSelector: is a class method and determines if instances of a class respond to a selector. It allows testing for an instance method given a class and without having an instance of that class.

CRD
  • 52,522
  • 5
  • 70
  • 86