Best way to remove type warning...

Discussion in 'iPhone/iPad Programming' started by ArtOfWarfare, Aug 8, 2011.

  1. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    Here's the line of code:
    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.
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Simply recast it into the desired type:
    Code:
    UILabel *scoreLabelToAdd = [COLOR="Red"][B](UILabel *)[/B][/COLOR][itemView viewWithTag:([[foundItemArray objectAtIndex:2] intValue] + 10)];
     
  3. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Thanks!

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

Share This Page