objective-c 等效于 -respondsToSelector 的类方法:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1135366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:55:32  来源:igfitidea点击:

Class method equivalent of -respondsToSelector:

objective-ccocoacocoa-touch

提问by firstresponder

Is there a class method equivalent to -respondsToSelector:?

是否有等效于的类方法-respondsToSelector:

Something like +respondsToSelector:?

+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)".

我问的原因是因为通过-respondsToSelector:在类级别上实现,我收到一个编译器警告:“在协议中找到 '-respondsToSelector:' 而不是 '+respondsToSelector:'”。

The code looks like this:

代码如下所示:

Class <SomeProtocol> someClass = [someInstance class];

if ([someClass respondsToSelector:@selector(someSelector:)]) {
    someVar = [someClass someSelector:someData];
}

回答by BJ Homer

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:

respondsToSelector:正如您可能知道的那样,类对象的响应很好。在测试应用程序中,我可以在没有任何编译器警告的情况下执行以下两项操作:

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 someClassas an idfor the purpose of calling respondsToSelector:. A somewhat cleaner solution would be to declare your own @protocolwhich declares +respondsToSelector:(SEL)selector, and then declare someClassas follows:

但是,您已经在变量上声明了一个协议,因此它假定您指向的类对象实现了这些方法。最简单的解决办法是投someClass作为一个id用于通话的目的respondsToSelector:。一个更简洁的解决方案是声明你自己的@protocolwhich declares +respondsToSelector:(SEL)selector,然后someClass如下声明:

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.

最后,请务必在http://bugreporter.apple.com 上向 Apple 提交错误。包括一个简单的测试应用程序,以便非常清楚您在做什么。他们欢迎这样的错误报告,即使它们过去已经提交过,因为这有助于他们确定修复的优先级。

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'trespond to -respondsToSelector:. -[NSObject respondsToSelector:]is actually declared in the NSObjectprotocol, not the class definition. The NSObjectprotocol is actually where most of what you know as NSObjectactually 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.

最后一点:这可能会发生,因为理论上,您可以选择实现一个与 NSObject 完全分开的根对象,在这种情况下,它不会响应-respondsToSelector:. -[NSObject respondsToSelector:]实际上是在NSObject协议中声明的,而不是在类定义中。该NSObject协议实际上是您所知道的大部分内容NSObject实际存在的地方。有人可能会争辩说,这+respondsToSelector:也应该在那里,但截至目前,还没有。由于您提供了一个协议列表,而该方法不在其中,因此它会向您发出警告以确保您知道自己在做什么。

回答by newacct

Well a class method is just a method of the class object, so you should be able to just do this

那么类方法只是类对象的方法,所以你应该能够做到这一点

[MyClass respondsToSelector:@selector(...)]

回答by Hugo Scott-Slade

You can use the following instancesRespondToSelector:since iOS 2.0, so where with an instance of a class you can do;

instancesRespondToSelector:从 iOS 2.0 开始,您可以使用以下内容,因此您可以使用类的实例来做什么;

[myInstance respondsToSelector: @selector(...)];

With a class you can use

使用一个类,您可以使用

[myClass instanceRespondsToSelector: @selector(...)];
// or
[[myInstance class] instanceRespondsToSelector: @selector(...)];

Which will behave like +(BOOL) respondsToSelector

哪个会表现得像 +(BOOL) respondsToSelector

回答by escouten

What I think you were asking is: Can you ask a class if it responds to +someMethodor not? In other words, thinking of the Cocoa Touch APIs, you would want:

我想你问的是:你能问一个班级是否有反应+someMethod吗?换句话说,考虑到 Cocoa Touch API,您会想要:

[ [ UIView class ] respondsToSelector: @selector( buttonWithType: ) ] -> NO
[ [ UIButton class ] respondsToSelector: @selector( buttonWithType: ) ] -> YES

But what I wrote above doesn't work as desired. respondsToSelector:is only about instance methods. (Thus both calls will return NO.) Within the Cocoa APIs there is no equivalent to respondsToSelector:for a class.

但是我上面写的东西不能按预期工作。respondsToSelector:仅与实例方法有关。(因此,两个调用都将返回 NO。)在 Cocoa API 中,没有对应respondsToSelector:的类。

You can, however, call class_getClassMethod. If the result is non-NULL, the class method you are asking about ispresent and you can call it.

但是,您可以调用class_getClassMethod。如果结果不为NULL,则是问有关的类方法礼物,你可以调用它。

回答by SK9

In Objective C, classes are also objects so you can send the object messages. In particular, you can ask -respondsToSelector:of a class. You can't send class level methods to non-class objects though.

在 Objective C 中,类也是对象,因此您可以发送对象消息。特别是,你可以问-respondsToSelector:一个类。但是,您不能将类级别的方法发送到非类对象。