views:

59

answers:

3

hi,

if i want to check whether a textfield or string is empty i compare it with NULL or nil?

thks

+1  A: 

Neither of the methods you suggest are foolproof. The best tests are:

if ([myTextField.text length] > 0) ...

or

  if ([myString length] > 0) ...
Run Loop
i tested and found comparing string length is much better. Thks for the help
Stefan
If my answer helped please mark it as answered. Thanks
Run Loop
A: 

I had a similar problem but no method other than this worked for me:

NSString *string = textfield.text;
if ([string isEqualToString:@""]) {
    ....
}
esqew
+2  A: 

if i want to check whether a textfield or string is empty i compare it with NULL or nil?

No.

An empty string object (a string object containing no characters) or a text-field object containing an empty string object is not the same as nil, which is no object at all. You need to ask the (text field's) string how long it is, or ask it whether it is equal to an empty string you have on hand (@"").

NULL, while also a null pointer, should be used for general pointers, not pointers to Objective-C instances (for which you have the more specific nil) or classes (for which you have the more specific Nil).

Peter Hosey