Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop warning for UIView may not respond to selector

I have a class that has a UIView as a property. Sometimes I pass in a UILabel; sometimes a UITextField. No matter which I pass in, I want the class to set the text. Currently I am doing this, which works:

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

The problem is, this gives a warning, because even though I'm checking respondsToSelector, Xcode doesn't know that my UIView will respond to setText:. How can I remove this warning?

I know that I can specifically check to see if it's a TextField or a Label, and then cast to a TextField or a Label, respectively, but this would be a pain, and if I ever have more types of views, I'd have to add a few more lines of code for each one.

I thought about creating my own protocol, and then having my class have id as the type for viewToUpdate... but of course UITextField and UILabel wouldn't conform to that protocol...

like image 301
GendoIkari Avatar asked Dec 02 '10 22:12

GendoIkari


1 Answers

try just casting it as an id:

if ([self.viewToUpdate respondsToSelector:@selector(setText:)] && !self.newAnswer)
    [(id)self.viewToUpdate setText:[[self.choices objectAtIndex:indexPath.row] text]];
like image 128
Sam Ritchie Avatar answered Sep 24 '22 23:09

Sam Ritchie