Select to view content in your preferred language

Text Field (Newbie question)

1015
2
Jump to solution
02-29-2012 09:25 AM
VladislavZagorodnyuk
New Contributor
Hi guys,i have next code, where answerText is UITextField
 if (answerText.text == @"1")      {         NSLog(@"YES");     }     else NSLog(@"NO");


this code always returns NO when i type in textField "1" (in iOS Simulator),it returns YES only when i type in Xcode answerText.text = @"1",why?
0 Kudos
1 Solution

Accepted Solutions
Nicholas-Furness
Esri Regular Contributor
Hi guys,i have next code, where answerText is UITextField
 if (answerText.text == @"1")      {         NSLog(@"YES");     }     else NSLog(@"NO");


this code always returns NO when i type in textField "1" (in iOS Simulator),it returns YES only when i type in Xcode answerText.text = @"1",why?


Hi.

You're comparing pointers to two strings. Remember, @"1" is just a shortcut to create a brand new NSString object containing the single character '1'. Both strings *look* the same, but they are separate objects.

Instead use isEqualToString: (see Apple's documentation).
if ([answerText.text isEqualToString:@"1"]) {     NSLog(@"YES"); } else NSLog(@"NO");


Cheers,

Nick.

UPDATE: I just happened across this road map from Apple, and this page seems particularly relevant.

View solution in original post

0 Kudos
2 Replies
Nicholas-Furness
Esri Regular Contributor
Hi guys,i have next code, where answerText is UITextField
 if (answerText.text == @"1")      {         NSLog(@"YES");     }     else NSLog(@"NO");


this code always returns NO when i type in textField "1" (in iOS Simulator),it returns YES only when i type in Xcode answerText.text = @"1",why?


Hi.

You're comparing pointers to two strings. Remember, @"1" is just a shortcut to create a brand new NSString object containing the single character '1'. Both strings *look* the same, but they are separate objects.

Instead use isEqualToString: (see Apple's documentation).
if ([answerText.text isEqualToString:@"1"]) {     NSLog(@"YES"); } else NSLog(@"NO");


Cheers,

Nick.

UPDATE: I just happened across this road map from Apple, and this page seems particularly relevant.
0 Kudos
VladislavZagorodnyuk
New Contributor
Thank you!
0 Kudos