我可以忽略这个iPhone警告吗?

时间:2010-09-09 09:33:50

标签: iphone warnings mkannotation

我有这段代码:

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

我收到警告:

  

' - 在协议中找不到标签

很公平,但我已经使用具有合成int tag变量的协议创建了一个新对象。

编辑:找到应用程序崩溃的原因 - 不是这一行。现在我收到警告,应用程序运行正常。

由于 汤姆

1 个答案:

答案 0 :(得分:4)

生成警告是因为对于annotationMKAnnotation静态类型,没有方法-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];
    }