xcode UITextField:测试空字符串或 nil 或 null 的文本属性不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3454553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextField : testing text property for empty string or nil or null doesn't work
提问by Steve S.
I got this weird problem testing for empty (or null) text property. Here my setup : I got a view with 6 textfield in it and here the code I use to go thru those field (loaded in a NSMutable Array)...
我在测试空(或空)文本属性时遇到了这个奇怪的问题。这是我的设置:我有一个包含 6 个文本字段的视图,这里是我用来遍历这些字段的代码(加载到 NSMutable 数组中)...
NSEnumerator *portsEnumerator = [appliancePorts objectEnumerator];
UITextField *tmpField;
newSite.port = [NSMutableArray array];
while (tmpField =[portsEnumerator nextObject]) {
NSLog(@"value:%@",tmpField.text);
if (![tmpField.text isEqualToString:nil]) {
[newSite.port addObject:(NSString *)tmpField.text];
}
}
When I'm in this interface and type some text in the first two field and "just" tab the remaning field and it the "Done" button here's what I got from GDB output:
当我在此界面中并在前两个字段中键入一些文本并“仅”选项卡剩余字段和“完成”按钮时,这是我从 GDB 输出中获得的内容:
2010-08-10 20:16:54.489 myApp[4883:207] value:Value 1
2010-08-10 20:16:58.115 myApp[4883:207] value:Value 2
2010-08-10 20:17:02.002 myApp[4883:207] value:
2010-08-10 20:17:13.034 myApp[4883:207] value:
2010-08-10 20:17:15.854 myApp[4883:207] value:
2010-08-10 20:17:17.762 myApp[4883:207] value:
I know that if I test for empty string it should work because the text property when dump to the console show this :
我知道如果我测试空字符串,它应该可以工作,因为转储到控制台时的文本属性显示:
UITextField: 0x5d552a0; frame = (20 8; 260 30); text = ''; clipsToBounds = YES; opaque = NO; tag = 1; layer = CALayer: 0x5d54f20
But the REAL problem begin when I go back the view, enter some text in the same first two field and it the "Done" button right after (not going thru the other field so they don't get any focus). This is again the GDB output...
但是当我返回视图时,真正的问题就开始了,在相同的前两个字段中输入一些文本,然后是“完成”按钮(不通过另一个字段,所以他们没有得到任何焦点)。这又是 GDB 输出...
2010-08-10 20:23:27.902 myApp[4914:207] value:Value 1
2010-08-10 20:23:31.739 myApp[4914:207] value:Value 2
2010-08-10 20:23:34.523 myApp[4914:207] value:(null)
2010-08-10 20:23:56.443 myApp[4914:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*-[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 2'
2010-08-10 20:23:56.443 myApp[4914:207] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[NSMutableArray insertObject:atIndex:]:尝试在 2'处插入 nil 对象
So, the obvious problems is that first the isEqualtoString:nil
doesn't work and second, how come this text change from '' to null in just of matter of putting the focus on the field.
所以,显而易见的问题是,首先isEqualtoString:nil
不起作用,其次,这个文本如何从 '' 变为 null 只是将焦点放在该领域的问题上。
So, is there a better way to test for empty field ?
那么,有没有更好的方法来测试空字段?
Thanks!
谢谢!
回答by Helen
I tend to use
我倾向于使用
if (![tmpField.text length])
This will miss it out if it either nil or @"". i.e. it finds the length of the string, and if it is 0 (which would be the case if the string were empty or nil) it does not execute the IF command.
如果它为零或@"",这将错过它。即它找到字符串的长度,如果它是 0(如果字符串为空或 nil 就是这种情况)它不执行 IF 命令。
回答by Adam Eberbach
How about
怎么样
if (![tmpField.text isEqualToString:@""])
- that tests for an empty string, not a nil string.
- 测试空字符串,而不是 nil 字符串。
There are many other ways including testing the length of the string. Note you can send a message to a nil object with no problems, so if tmpField.text was nil then the test would succeed (and you would crash when trying to add a nil object to your array) - but the test you want is to send a message to the NSString object querying whether it is empty or not.
还有许多其他方法,包括测试字符串的长度。请注意,您可以毫无问题地向 nil 对象发送消息,因此如果 tmpField.text 为 nil,则测试将成功(并且在尝试将 nil 对象添加到数组时会崩溃)-但您想要的测试是向 NSString 对象发送一条消息,查询它是否为空。