如何停止UIView警告可能无法响应选择器

Gen*_*ari 9 xcode objective-c compiler-warnings ios

我有一个UIView作为属性的类.有时我传入UILabel; 有时是UITextField.无论我传入哪个,我都希望课程设置文本.目前我这样做,这有效:

if ([self.viewToUpdate respondsToSelector:@selector(setText:)] && !self.newAnswer)
    [self.viewToUpdate setText:[[self.choices objectAtIndex:indexPath.row] text]];
Run Code Online (Sandbox Code Playgroud)

问题是,这给出了警告,因为即使我正在检查respondsToSelector,Xcode也不知道我的UIView会响应setText:.如何删除此警告?

我知道我可以专门检查它是TextField还是Label,然后分别转换为TextField或Label,但这会很痛苦,如果我有更多类型的视图,我会有为每一个添加几行代码.

我想创建自己的协议,然后让我的类有id作为viewToUpdate的类型...但当然UITextField和UILabel不符合该协议...

Sam*_*hie 16

尝试将其作为id转换:

if ([self.viewToUpdate respondsToSelector:@selector(setText:)] && !self.newAnswer)
    [(id)self.viewToUpdate setText:[[self.choices objectAtIndex:indexPath.row] text]];
Run Code Online (Sandbox Code Playgroud)