The Blog

UITextField.text is not always there

If you have UITextField on the page and you need to validate is there something in it (so if yes you can save the input or something), you might do something like this:

OBJC:
  1. if (![addNewCellTextField.text isEqualToString:@""]) {
  2. }

However, this will fail if the field is never touched. That is, until your user taps the field and it gets focus, UITextField.text is nil. Which means that condition above yields true, which is not what you want.
Once it is touched though, this becomes an instance of NSString with value of @””. Thus, always use:

OBJC:
  1. if (addNewCellTextField.text != nil && ![addNewCellTextField.text isEqualToString:@""]) {
  2. }

Watch out for this little trap hole.

Banca

Banca

Beautiful and functional currency converter, supports just about any currency in the world.

Go Couch to 5k

Go Couch to 5k

The most popular starter running program in beautiful feature-rich app (GPS tracking, charts, detailed history etc)

Quickie to do

Quickie to do

The fastest short-term task-list / check-list app on the App Store. Really.

Guerrilla Cardio

Guerrilla Cardio

The most challenging high-impulse interval training in the world.

Run Mate

Run Mate

A versatile running coach app, with unlimited number of running programs. Perfect for casual runners.

One Comment

Feel free to chime in, looking forward to it. Leave a Comment

  1. Brandon says:

    A simpler way of doing this would be

    if ([textField.text length] != 0) { .… }

    then it doesn’t matter if textField.text is nil.

Leave a Comment

Add your comment here, just please be civilized and stay on topic.