iPhone string comparison strange issue in iOS 6

流过昼夜 提交于 2019-12-12 09:18:27

问题


I used to compare the text entered in a txt field as below,

if ([myTextfield.text isEqualToString: @""])
{
 // success code
}
else
{
 // my code
}

in iOS 7 this check works perfectly. If nothing entered in to the text field, condition is success. But in iOS 6 if the field is blank, this condition always false and moves to the else block.

So i did like,

if (myTextfield.text.length == 0)
{
 // success code
}
else
{
 // my code
}

This works fine, and I just wanted to know what is wrong in my first method.


回答1:


If myTextfield.text is nil, [myTextfield.text isEqualToString: @""] will fail because messaging nil returns nil (or 0, NO as appropriate).

In the second case you have, you are checking for == 0, so even if the string is nil you will still get a positive result.




回答2:


In iOS7, untouched UITextFields return nil, whereas in previous iOS versions they return an empty string. Touched UITextFields in both cases should return an empty string.

(Did you ask the question in reverse mistaking iOS6 w 7? If not, I'd also make sure the text field is hooked up properly since a touched iOS7 text field could return an empty string while an unsynthesized iOS6 text field could return NULL since iOS6 is especially strict in this way.)



来源:https://stackoverflow.com/questions/21605075/iphone-string-comparison-strange-issue-in-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!