简体   繁体   English

类方法等效于-respondsToSelector:

[英]Class method equivalent of -respondsToSelector:

Is there a class method equivalent to -respondsToSelector: ? 是否有一个等效于-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:'而不是'protocol中的'respondsToSelector:'”。

The code looks like this: 代码如下所示:

Class <SomeProtocol> someClass = [someInstance class];

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

Update after seeing your edit: 看到您的修改后更新:

A class object responds to respondsToSelector: just fine, as you're probably aware. 一个类对象响应respondsToSelector:就好了,你可能已经知道了。 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: . 最简单的解决方案是将someClassid ,以便调用respondsToSelector: . A somewhat cleaner solution would be to declare your own @protocol which declares +respondsToSelector:(SEL)selector , and then declare someClass as follows: 一个更清晰的解决方案是声明你自己的@protocol声明+respondsToSelector:(SEL)selector ,然后声明someClass如下:

Class<SomeProtocol, ClassRespondingToSelector> someClass = ...

Finally, be sure to file a bug with Apple at http://bugreporter.apple.com . 最后,请务必在http://bugreporter.apple.com上向Apple提交错误。 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: . -[NSObject respondsToSelector:] is actually declared in the NSObject protocol, not the class definition. -[NSObject respondsToSelector:]实际上是在NSObject协议中声明的,而不是类定义。 The NSObject protocol is actually where most of what you know as NSObject actually lives. NSObject协议实际上是你所知道的NSObject大部分实际存在的地方。 One could argue that +respondsToSelector: should also be in there, but as of now, it's not. 有人可能认为+respondsToSelector:也应该在那里,但截至目前,它不是。 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. 由于您提供了协议列表,并且该方法不在其中,因此它会向您发出警告,以确保您知道自己在做什么。

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

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

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

What I think you were asking is: Can you ask a class if it responds to +someMethod or not? 我认为你问的是:你能问一个班级是否回应+someMethod In other words, thinking of the Cocoa Touch APIs, you would want: 换句话说,想到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. respondsToSelector:仅与实例方法有关。 (Thus both calls will return NO.) Within the Cocoa APIs there is no equivalent to respondsToSelector: for a class. (因此两个调用都将返回NO。)在Cocoa API中,对于一个类,没有与respondsToSelector:等效的东西。

You can, however, call class_getClassMethod . 但是,您可以调用class_getClassMethod If the result is non-NULL, the class method you are asking about is present and you can call it. 如果结果为非NULL, 表示您要询问的类方法,并且可以调用它。

In Objective C, classes are also objects so you can send the object messages. 在Objective C中,类也是对象,因此您可以发送对象消息。 In particular, you can ask -respondsToSelector: of a class. 特别是,你可以问一个类的-respondsToSelector: . You can't send class level methods to non-class objects though. 但是,您不能将类级方法发送到非类对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM