PDA

View Full Version : Stupid Question. String values




ErikMadsen
Nov 11, 2008, 04:58 PM
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.



AussieSusan
Nov 11, 2008, 05:44 PM
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:@""] && NO == [txtPassword.text isEqualTo:@""])

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

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

Susan

ErikMadsen
Nov 11, 2008, 05:53 PM
Many thanks, Susan! *removes dunce cap and returns to desk*


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





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:@""] && NO == [txtPassword.text isEqualTo:@""])

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

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

Susan

PhoneyDeveloper
Nov 11, 2008, 09:59 PM
isEqualTo: doesn't exist on the phone. Use isEqualToString: Even down unda.