Stupid Question. String values

Discussion in 'iPhone/iPad Programming' started by ErikMadsen, Nov 11, 2008.

  1. macrumors newbie

    Joined:
    Feb 26, 2008
    #1
    I'm new to C/ObjC and am wondering why this statement is always true when I known darn well there's no text in the UITextFields referenced...

    Code:
    if(txtEmail.text != @"" && txtPassword.text != @""){
    ...
    }
    
    Many thanks. I'll go back to my corner and put my dunce cap back on until an answer arrives.
     
  2. macrumors member

    Joined:
    May 29, 2006
    Location:
    Melbourne, Australia
    #2
    Objective-C strings are actually an instance of the NSString class - even the @"xxx" literals. Therefore what you are comparing is the addresses of the class instances and so both sides of each '!=' will always be different and so the '!=' will always 'true'.

    What you want is something like (of the top of my head):
    if( NO == [txtEmail.text isEqualTo:mad:""] && NO == [txtPassword.text isEqualTo:mad:""])

    An alternative is:
    if(* NSOrderedSame != [txtEmail.text compare:mad:""].........

    (I suggest you look up the isEqualTo: and compare: functions to get the syntax right - I'm just going form memory).

    Susan
     
  3. thread starter macrumors newbie

    Joined:
    Feb 26, 2008
    #3
    Many thanks, Susan! *removes dunce cap and returns to desk*

    Code:
    if(![txtEmail.text isEqualToString:@""] && ![txtPassword.text isEqualToString:@""]){
    [btnLogin setEnabled:YES];
    }
    



     
  4. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    isEqualTo: doesn't exist on the phone. Use isEqualToString: Even down unda.
     

Share This Page