xcode self.delegate respondsToSelector: ... 不编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9579337/
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-14 23:32:31  来源:igfitidea点击:

self.delegate respondsToSelector: ... does not compile

xcodeios5

提问by vmanjz

I've implemented a protocol with an optional method and in the calling method I want to send respondsToSelector:to self.delegatebefore I send the message, but that does not compile. The fail message is:

我已经使用可选方法实现了一个协议,并且在我发送消息之前要发送respondsToSelector:到的调用方法中self.delegate,但它无法编译。失败消息是:

No Known instance method for selector 'respondsToSelector'.

选择器 'respondsToSelector' 没有已知的实例方法

As a work-around, I "sanitized" the delegate as shown below, which compiles...

作为一种解决方法,我“清理”了如下所示的委托,它编译...

//MyClass.h:

@class MyClass;

@Protocol MyClassDelegate
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
@end

@interface MyClass : UIViewController

@property (nonatomic, weak) id<MyClassDelegate> delegate;

@end

and

//MyClass.m:

...
@synthesize delegate = _delegate;
...

id sanitizedDelegate = self.delegate; //Hmmmm... why does this work?

if ([sanitizedDelegate respondsToSelector:@selector(myClass:willDoSomething:)]) {
    [self.delegate myClass:self willDoSomething:animated];
}

.

.

I checked a number of posts including this onebut it does not answer the compilation fail issue.

我检查了许多帖子,包括这个帖子,但它没有回答编译失败问题。

Also, alternative accessors do not work...

此外,替代访问器不起作用......

[self delegate]
//or
_delegate

Has anyone seen this or can advise a better way of handling?

有没有人看到这个或可以建议更好的处理方式?

IOS 5.0:(9A334), Xcode 4.2.1 (4D502)

IOS 5.0:(9A334), Xcode 4.2.1 (4D502)

回答by Kurt Revis

-respondsToSelector:is a method on NSObject. Either assume that your iddelegate is in fact an NSObject, and cast it:

-respondsToSelector:是 NSObject 上的一个方法。要么假设您的id委托实际上是一个 NSObject,然后将其转换为:

[(NSObject*)self.delegate respondsToSelector:@selector(myClass:willDoSomething:)]

[(NSObject*)self.delegate respondsToSelector:@selector(myClass:willDoSomething:)]

Or, better, make your delegate explicitly an NSObject:

或者,更好的是,让您的委托明确成为 NSObject:

@property (nonatomic, weak) NSObject<MyClassDelegate>* delegate;

@property (nonatomic, weak) NSObject<MyClassDelegate>* delegate;

Or make the protocol be a sub-protocol of NSObject:

或者让协议成为 NSObject 的子协议:

@protocol MyClassDelegate <NSObject>

@protocol MyClassDelegate <NSObject>

回答by NJones

Basically you are saying that your delegate is constrained only by your <MyClassDelegate>protocol so the compiler assumes that those are the only methods available. What you need to do is have the protocol extend <NSObject>like so:

基本上,您是说您的委托仅受您的<MyClassDelegate>协议约束,因此编译器假定这些是唯一可用的方法。你需要做的是让协议<NSObject>像这样扩展:

@Protocol MyClassDelegate <NSObject>
- (void)myClass:(MyClass *)sender willDoSomething:(BOOL)animated;
@end

That way the compiler knows that any object which conforms to your protocol also conforms to the <NSObject>protocol which defines respondsToSelector:.

这样编译器就知道任何符合您的协议的对象也符合<NSObject>定义respondsToSelector:.