问题描述
是否有等价于-respondsToSelector:
的类方法?
Is there a class method equivalent to -respondsToSelector:
?
类似于 +respondsToSelector:
?
我问的原因是因为通过在类级别上实现 -respondsToSelector:
,我收到了编译器警告:在协议中找到了‘-respondsToSelector:’而不是‘+respondsToSelector:’(s)".
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)".
代码如下:
Class <SomeProtocol> someClass = [someInstance class];
if ([someClass respondsToSelector:@selector(someSelector:)]) {
someVar = [someClass someSelector:someData];
}
推荐答案
看到您的编辑后更新:
类对象响应 respondsToSelector:
就好了,你可能知道.在测试应用程序中,我可以在没有任何编译器警告的情况下执行以下两项操作:
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:)]);
但是,您已经在变量上声明了一个协议,因此它假定您指向的类对象实现了这些方法.最简单的解决方案是将 someClass
转换为 id
以调用 respondsToSelector:
.一个更简洁的解决方案是声明你自己的 @protocol
,它声明 +respondsToSelector:(SEL)selector
,然后声明 someClass
如下:
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 = ...
最后,请务必在 http://bugreporter.apple.com 上向 Apple 提交错误.包括一个简单的测试应用程序,以便非常清楚您在做什么.他们欢迎此类错误报告,即使它们过去曾提交过,因为这有助于他们确定修复的优先级.
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.
最后一点:这可能会发生,因为理论上,您可以选择实现一个与 NSObject 完全分开的根对象,在这种情况下,它不会响应 -响应选择器:
.-[NSObject RespondsToSelector:]
实际上是在 NSObject
协议中声明的,而不是类定义中.NSObject
协议实际上是您所知的大部分 NSObject
实际存在的地方.有人可能会争辩说 +respondsToSelector:
也应该在那里,但截至目前,它不是.由于您提供了一个协议列表,而该方法不在其中,因此它会向您发出警告,以确保您知道自己在做什么.
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.
这篇关于等效于 -respondsToSelector 的类方法:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!