Hello. I'm Aleksandar Vacić, professional web developer and wine maker in the making.
Learn more about me or see what I can do for you.

My work & services

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.

tags: Programming
Comments: 1

Posted 7 months, 4 weeks agopermanent link

trackback URL: http://aplus.rs/programming/uitextfield-text-is-not-always-there/trackback/

Voices from the crowd, one so far

A simpler way of doing this would be

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

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

by Brandon
3 weeks, 3 days after the post

Post your opinion


? You need to enter your email, but rest assured it will not be published.

? Enter the link and it will be published.
? This blog strives to behave properly, as per XHTML 1.0 Strict spec. You can use the following tags, but please use them wisely:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
? I have enabled comment moderation so if your comment does not shows up after you submit it, don't worry. I will review it and if it's nice and to the point, I'll approve it. This measure is up mostly to fight spam and trolls.

Tags or categories or topics...

Lots of ramblings on this blog...might be easier for you to find your juice through these tags:

Post a job. Find one. authenticjobs.com