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
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:@""]) {
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With