PDA

View Full Version : Best way to remove type warning...




ArtOfWarfare
Aug 8, 2011, 02:07 PM
Here's the line of code:
UILabel *scoreLabelToAdd = [itemView viewWithTag:([[foundItemArray objectAtIndex:2] intValue] + 10)];

viewWithTag: returns a UIView. Assuming my code works properly, it should return, more specifically, a UILabel. I need to specify that it's a UILabel here, because otherwise later on when I ask for the text property it says that UIViews don't have a text property.

So, what suggestions are there for removing the warning? What's the best programming practice in a case like this? Make an all new subclass of UIView that returns a UILabel when it's asked for a labelWithTag: or something like that? (Seems silly given the existing class does nearly exactly what I want.)

Thanks for any help.



dejo
Aug 8, 2011, 02:27 PM
Here's the line of code:
UILabel *scoreLabelToAdd = [itemView viewWithTag:([[foundItemArray objectAtIndex:2] intValue] + 10)];

Simply recast it into the desired type:
UILabel *scoreLabelToAdd = (UILabel *)[itemView viewWithTag:([[foundItemArray objectAtIndex:2] intValue] + 10)];

ArtOfWarfare
Aug 8, 2011, 07:20 PM
Thanks!

I'd been doing it wrong; I had thought castes were done something like this:
(UILabel *)[scoreLabelToAdd.text intValue]

Silly me.