iOS 6中的iPhone字符串比较奇怪的问题 [英] iPhone string comparison strange issue in iOS 6

查看:68
本文介绍了iOS 6中的iPhone字符串比较奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经比较在txt字段中输入的文字如下所示,

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

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

iOS 7中的

此检查完美无缺。如果没有输入文本字段,则条件成功。但是在iOS 6中,如果该字段为空,则此条件始终为false并移至else块。

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.

所以我喜欢,

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.

推荐答案

如果 myTextfield.text nil [myTextfield.text isEqualToString:@] 将失败,因为消息传递nil返回nil(或0, NO 视情况而定)。

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

在第二种情况下,你要检查 == 0 ,所以即使字符串是 nil ,你仍然会得到一个肯定的结果。

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.

这篇关于iOS 6中的iPhone字符串比较奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆