Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField is returning nil when it is empty

Tags:

ios

ios7

I am preparing my app to iOS7 and have a weird problem:

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

Is that a formal change or bug ?

Thanks Shani

like image 694
shannoga Avatar asked Sep 11 '13 12:09

shannoga


3 Answers

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

@property (weak, nonatomic) IBOutlet UITextField *tf;

// iOS6
if (![self.tf.text isEqualToString:@""]){

// iOS7    
if (self.tf.text != nil && ![self.tf.text isEqualToString:@""]) {
like image 117
marsei Avatar answered Nov 08 '22 10:11

marsei


Another thing which I think you can do is :-

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

The above will handle both empty strings as well as nil. Since sending a length message to nil returns 0.

like image 41
Max Avatar answered Nov 08 '22 08:11

Max


Yes, Its a formal Change. So you need to handle like 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 for both iOS 7 and earlier version.

if([self.tf.text length] != 0)
{
// do your stuff
}
like image 5
Dharmbir Singh Avatar answered Nov 08 '22 08:11

Dharmbir Singh