我有这种代码:
我的类.h
#import "MyOtherClass.h"
@interface MyClass : NSObject<SomeProtocol, MyOtherClassDelegate> {
...
}
我的班级.m
+ (void) doThis {
[someObject doThisWithDelegate:self]; // someObject is MyOtherClass type
}
MyOtherClass.h:
@protocol MyOtherClassDelegate <NSObject>
@required
// Some methods
@end
- (void) doThisWithDelegate:(id<SomeOtherProtocol>)delegate;
MyOtherClass.m:
- (void) doThisWithDelegate:(id<SomeOtherProtocol>)delegate {
if ([delegate respondsToSelector:@selector(myProtocolMethod:error:)]) do things;
}
这样做,我在编译时有以下警告:
在线上[someObject doThisWithDelegate:self];
"Incompatible pointer types sending Class to parameter of type id<SomeOtherProtocol>"
在 MyOtherClass.h 中的方法 declaratéin 上:
"Passing argument to parameter 'delegate' here"
之前,我没有输入 id 参数(用id<SomeOtherProtocol>
),它只是“单独”(id
)。我注意到测试:
if ([delegate respondsToSelector:@selector(myProtocolMethod:error:)])
返回 FALSE(当然方法是在委托中实现和声明的)。
所以我决定尝试强制id
类型符合在编译时导致我警告的协议。
这里发生了什么?
为什么我有这个错误,为什么respondsToSelector
不返回 TRUE ?