忽略不正确的编译器警告?

时间:2012-12-28 20:22:21

标签: objective-c warnings compiler-warnings

我有通知及其处理程序:

- (void) addObservers
{
...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleColumnCreated:) name:NNColumnCreated object:nil];
…
}

- (void) handleColumnCreated:(NSNotification*)notification
{
    [_formFields makeInfoForColumn:[notification object] FieldInfo:_propertiesViewController.representedObject];
    [self setActiveColumn:[notification object]];
}

- (void) setActiveColumn:(id)theColumn
{
    if (_activeColumn != nil)
    {
        [_activeColumn setBackgroundColor:_oldColumnColor];
    }

    _activeColumn = theColumn;
    _oldColumnColor = [_activeColumn backgroundColor];
    [_activeColumn setBackgroundColor:[NSColor greenColor]];
    [_window makeFirstResponder:theColumn];

    [_propertiesViewController setRepresentedColumn:[theColumn info]];
}

在setActiveColumn的最后一行,我收到一条警告,说明我发送给setRepresentedColumn的参数:类型错误。然而,当我使用调试器跟踪行时,[theColumn info]解析为正确的类型并且行正确执行。

我可以忽略这个警告,但我认为这不是一个好主意。我无法弄清楚编译器认为[theColumn info]产生错误类型的对象的原因。救命啊!

1 个答案:

答案 0 :(得分:3)

您在info参数上调用的任何theColumn方法的声明类型与setRepresentedColumn:的参数的声明类型不匹配。您可以通过强制转换为正确的类型来防止此警告。

例如,如果声明setRepresentedColumn:

- (void)setRepresentedColumn:(MyColumnObject *)column

然后您可以将最后一行更改为:

[_propertiesViewController setRepresentedColumn:(MyColumnObject *)[theColumn info]];

但是,请注意,您所做的只是告诉编译器,“相信我,我知道这个对象是什么类型的”,让它闭嘴......如果{{{}你仍然容易出现运行时错误1}}永远不会返回[theColumn info]以外的其他内容。

相关问题