私はこの種のコードを持っています:
MyClass.h
#import "MyOtherClass.h"
@interface MyClass : NSObject<SomeProtocol, MyOtherClassDelegate> {
...
}
MyClass.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 のメソッド宣言で:
"Passing argument to parameter 'delegate' here"
以前は、id パラメーター ( id<SomeOtherProtocol>
) を入力していませんでした。単に「単独」 ( id
) でした。私はテストに気づいた:
if ([delegate respondsToSelector:@selector(myProtocolMethod:error:)])
FALSE を返しました (ただし、もちろんメソッドはデリゲートで実装および宣言されています)。
id
そのため、コンパイル時に警告が発生するプロトコルに型を強制的に準拠させることにしました。
ここで何が起きてるの ?
このエラーが発生するのはなぜですか?また、 respondsToSelector
TRUE が返されないのはなぜですか?