views:

102

answers:

2

Hi!

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!

+1  A: 

How about

if (![tmpField.text isEqualToString:@""])
  • that tests for an empty string, not a nil string.

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.

Adam Eberbach
This will work only if the field got the focus because it get a '' string. But if the field don't got the focus, then you got a null and this test fail.
Steve S.
+1  A: 

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.

Helen
+1 - much better way
jrtc27
This work, except for the negate at the beginning. Remove it and it work for '' or null string. Thanks! But still, I wonder why putting the focus on a textfield change is text value from null to '' ?!?
Steve S.