Stupid Question. String values

Discussion in 'iOS Programming' started by ErikMadsen, Nov 11, 2008.

  1. ErikMadsen
    Expand Collapse
    macrumors newbie

    Feb 26, 2008
    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...

    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. AussieSusan
    Expand Collapse
    macrumors member

    May 29, 2006
    Melbourne, Australia
    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).

  3. ErikMadsen
    Expand Collapse
    thread starter macrumors newbie

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

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

  4. PhoneyDeveloper
    Expand Collapse
    macrumors 68030


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

Share This Page