Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective c checking whether text field is empty

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.

like image 548
objectiveccoder001 Avatar asked Jul 04 '10 03:07

objectiveccoder001


2 Answers

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 */ } 
like image 86
Joshua Weinberg Avatar answered Sep 20 '22 19:09

Joshua Weinberg


We already have inbuilt method that return boolean value that indicates whether the text-entry objects has any text or not.

// In Obj-C if ([textField hasText]) {         //*    Do Something you have text     }else{          /* what ever */     }  // In Swift  if textField.hasText {     //*    Do Something you have text }else{      /* what ever */ } 
like image 24
Gaurav Pandey Avatar answered Sep 19 '22 19:09

Gaurav Pandey