I have a typical textfield in an iphone app, and in ViewWillAppear I would like to do a little logic using the text in the text field.
If txtField.text is empty, I would like the next textfield, txtField2 to be selected for typing.
I am trying to achieve this through :
if (txtField.text != @"") [txtField2 becomeFirstResponder];
but after some testing, txtField.text (whilst empty), will still not register as being equal to @””
What am I missing?
When you compare two pointer variables you actually compare the addresses in memory these two variables point to. The only case such expression is true is when you have two variables pointing to one object (and therefore to the very same address in memory).
To compare objects the common approach is to use
isEqual:defined inNSObject. Such classes asNSNumber,NSStringhave additional comparison methods namedisEqualToNumber:,isEqualToString:and so on. Here’s an example for your very situation:If you want to do something if a
NSStringis in fact not empty use!symbol to reverse the expression’s meaning. Here’s an example: