Is there a class method equivalent to -respondsToSelector:
?
Something like +respondsToSelector:
?
The reason I am asking is because by implementing -respondsToSelector:
on a class level, I get a compiler warning: "found '-respondsToSelector:' instead of '+respondsToSelector:' in protocol(s)".
The code looks like this:
Class <SomeProtocol> someClass = [someInstance class]; if ([someClass respondsToSelector:@selector(someSelector:)]) { someVar = [someClass someSelector:someData]; }
A class method is a method that is bound to a class rather than its object. It doesn't require creation of a class instance, much like staticmethod. The difference between a static method and a class method is: Static method knows nothing about the class and just deals with the parameters.
To call a class method, put the class as the first argument. Class methods can be can be called from instances and from the class itself. All of these use the same method. The method can use the classes variables and methods.
A class method is a method which is bound to the class and not the object of the class. They have the access to the state of the class as it takes a class parameter that points to the class and not the object instance. It can modify a class state that would apply across all the instances of the class.
Class methods are methods that are called on the class itself, not on a specific object instance. The static modifier ensures implementation is the same across all class instances. Many standard built-in classes in Java (for example, Math) come with static methods (for example, Math.
Update after seeing your edit:
A class object responds to respondsToSelector:
just fine, as you're probably aware. In a test application, I can do both of the following without any compiler warnings:
NSLog(@"Responds to selector? %i", [MyObject respondsToSelector:@selector(respondsToSelector:)]); NSLog(@"Responds to selector? %i", [[MyObject class] respondsToSelector:@selector(respondsToSelector:)]);
However, you've declared a protocol on your variable, so it assumes that the class object you're pointing to implements those methods. The simplest solution would be to cast someClass
as an id
for the purpose of calling respondsToSelector:
. A somewhat cleaner solution would be to declare your own @protocol
which declares +respondsToSelector:(SEL)selector
, and then declare someClass
as follows:
Class<SomeProtocol, ClassRespondingToSelector> someClass = ...
Finally, be sure to file a bug with Apple at http://bugreporter.apple.com. Include a simple test application so that it's very clear what you're doing. They welcome such bug reports, even if they've been submitted in the past, as it helps them prioritize the fixes.
Final note: this is probably happening because in theory, you could have chosen to implement a root object entirely separate from NSObject, and in that case, it wouldn't respond to -respondsToSelector:
. -[NSObject respondsToSelector:]
is actually declared in the NSObject
protocol, not the class definition. The NSObject
protocol is actually where most of what you know as NSObject
actually lives. One could argue that +respondsToSelector:
should also be in there, but as of now, it's not. And since you've provided a protocol list, and the method isn't in there, it gives you a warning to make sure you know what you're doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With