Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ErikMadsen

macrumors newbie
Original poster
Feb 26, 2008
15
3
Oxford, MD
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.
 

AussieSusan

macrumors member
May 29, 2006
54
0
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).

Susan
 

ErikMadsen

macrumors newbie
Original poster
Feb 26, 2008
15
3
Oxford, MD
Many thanks, Susan! *removes dunce cap and returns to desk*

Code:
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: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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.