0

私はこのコードを持っています:

if([annotation respondsToSelector:@selector(tag)]){
    disclosureButton.tag = [annotation tag];
}

そして私は警告を受け取ります:

'-tag'がプロトコルに見つかりません

int tag十分に公平ですが、合成された変数を持つプロトコルを使用して新しいオブジェクトを作成しました。

編集:アプリがクラッシュした理由を見つけました-この行ではありません。警告が表示され、アプリは正常に動作します。

ありがとうトム

4

1 に答える 1

4

の静的タイプannotation場合MKAnnotation、メソッドがないため、警告が生成され-tagます。動的タイプがセレクターに応答するかどうかをすでに確認しているので、この場合は警告を無視できます。

警告を取り除くには:

  • 特定のクラスが必要な場合は、代わりにテストできます。

    if ([annotation isKindOfClass:[TCPlaceMark class]]) {
        disclosureButton.tag = [(TCPlaceMark *)annotation tag];
    }
    
  • プロトコルの場合:

    if ([annotation conformsToProtocol:@protocol(PlaceProtocol)]) {
        disclosureButton.tag = [(id<PlaceProtocol>)annotation tag];
    }
    
  • または、両方が適用されない場合は、特定のプロトコルを使用して警告を抑制します(たとえば、Apple APIが急速に変化する場合に便利です)。

    @protocol TaggedProtocol
    - (int)tag;
    @end
    
    // ...
    if([annotation respondsToSelector:@selector(tag)]){
        disclosureButton.tag = [(id<TaggedProtocol>)annotation tag];
    }
    
于 2010-09-09T10:08:55.573 に答える