hi,
if i want to check whether a textfield or string is empty i compare it with NULL or nil?
thks
hi,
if i want to check whether a textfield or string is empty i compare it with NULL or nil?
thks
Neither of the methods you suggest are foolproof. The best tests are:
if ([myTextField.text length] > 0) ...
or
if ([myString length] > 0) ...
I had a similar problem but no method other than this worked for me:
NSString *string = textfield.text;
if ([string isEqualToString:@""]) {
....
}
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
).