UITextField:测试文本属性是否为空字符串或 nil 或 null 不起作用
我在测试空(或空)文本属性时遇到了这个奇怪的问题。 这是我的设置:我得到了一个包含 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];
}
}
当我在此界面中并在前两个字段中键入一些文本时并“只需”选项卡剩余字段及其“完成”按钮,这是我从 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:
我知道如果我测试空字符串它应该可以工作,因为转储到控制台时的文本属性显示此:
UITextField: 0x5d552a0; frame = (20 8; 260 30); text = ''; clipsToBounds = YES; opaque = NO; tag = 1; layer = CALayer: 0x5d54f20
但是真实的当我返回视图时,问题开始了,在相同的前两个字段中输入一些文本,然后输入“完成”按钮(不通过其他字段,因此它们不会获得任何焦点)。这又是 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] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSMutableArray insertObject:atIndex:]:尝试在 2' 处插入 nil 对象
所以,明显的问题是,首先 isEqualtoString:nil 不起作用,其次,为什么这个文本只是将焦点放在领域。
那么,有没有更好的方法来测试空场呢?
谢谢!
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)...
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:
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...
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'
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.
So, is there a better way to test for empty field ?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于使用
This 会错过它,如果它是 nil 或 @""。即它找到字符串的长度,如果它是 0(如果字符串为空或 nil 就会出现这种情况),它不会执行 IF 命令。
I tend to use
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.
怎么样
?还有许多其他方法,包括测试字符串的长度。请注意,您可以毫无问题地向 nil 对象发送消息,因此如果 tmpField.text 为 nil 那么测试将会成功(并且在尝试将 nil 对象添加到数组时会崩溃) - 但您想要的测试是向 NSString 对象发送一条消息,查询它是否为空。
How about
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.