Objective c checking whether text field is empty

objectiveccoder001 picture objectiveccoder001 · Jul 4, 2010 · Viewed 66k times · Source

Here's the code:

- (IBAction) charlieInputText:(id)sender {
    //getting value from text field when entered
    charlieInputSelf = [sender stringValue];

    if (charlieInputSelf != @"") {
        //(send field if not empty
    }
}    

This sends it even when the field is empty; therefore, this does not work as I want it to.

Answer

Joshua Weinberg picture Joshua Weinberg · Jul 4, 2010

Simply checks for nil and if length of text length is greater than 0 - not empty

if (textField.text && textField.text.length > 0)
{
   /* not empty - do something */
}
else
{
   /* what ever */
}