If Else, statement not working properly.

Discussion in 'iOS Programming' started by jjgraz, Apr 18, 2009.

  1. macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    Any Ideas why my If Else Statement is not working? If I change the statment to read, if 1 == 1, it works fine. I'm sure alabel.status is bringing 50,(the debugger shows its returning 50). But alabel.status for some reason is not equal to @"50".....the same is true if i Return a word and not a number. Thank you. Code below.

    if (alabel.status == @"50") {

    UIImage *image = [UIImage imageNamed:mad:"image1.png"];
    cell.image = image;

    }

    else {
    UIImage *image = [UIImage imageNamed:mad:"image2.png"];
    cell.image = image;

    }


    Thank you.
     
  2. macrumors newbie

    Joined:
    Apr 13, 2009
    #2
    I'm assuming alabel.status is a string. In order to compare strings, you have to use the isEqualToString command. It will look something like this:

    if ([[NSString stringWithFormat:alabel.status] isEqualToString: @"50"]) {
    UIImage *image = [UIImage imageNamed:mad:"image1.png"];
    cell.image = image;

    }

    else {
    UIImage *image = [UIImage imageNamed:mad:"image2.png"];
    cell.image = image;

    }

    Hopefully that will take care of it for you!
     
  3. thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
  4. macrumors 603

    chrmjenkins

    Joined:
    Oct 29, 2007
    Location:
    CA
    #4
    This is a common mistake in a lot of programming languages :D

    Just remember to ask yourself if the statement is comparing the actual data, or the pointer to the data.
     
  5. macrumors 68010

    Joined:
    Aug 23, 2008

Share This Page