TextField returns zero when it is empty

I am preparing my application for iOS7 and have a strange problem:

When I try to get empty TextField text, I get "nil", while in the past it used to return @ "" (empty string).

Is this a formal change or mistake?

Thanks Shani

+8
ios ios7
source share
3 answers

This is a formal change from iOS6 to iOS7. The text field used to return an empty string, but now you use the string nil instead.

 @property (weak, nonatomic) IBOutlet UITextField *tf; // iOS6 if (![self.tf.text isEqualToString:@""]){ // iOS7 if (self.tf.text != nil && ![self.tf.text isEqualToString:@""]) { 
+10
source share

Another thing that I think you can do is: -

 if([self.tf.text length] != 0) { // do whatever } 

The above will handle both empty lines and nil. Since sending a message of length in nil returns 0.

+6
source share

Yes, this is a formal change. So you need to handle this

 // For iOS 6 if (![self.tf.text isEqualToString:@""]) { } // For iOS 7 if (self.tf.text != nil && [self.tf.text length] != 0) { } 

or

This condition is for iOS 7 and earlier.

 if([self.tf.text length] != 0) { // do your stuff } 
+5
source share

All Articles