Objective c checking whether text field is empty

后端 未结 8 1147
孤独总比滥情好
孤独总比滥情好 2021-02-05 01:11

Here\'s the code:

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

           


        
相关标签:
8条回答
  • 2021-02-05 01:39

    Those 'work' in a way. However, I found that the user can just fill in the box with spaces. I found using regular expressions helps (though what I use is for words without spaces) I can't really figure out how to make allow spaces.

    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[ ]" options:NSRegularExpressionCaseInsensitive error:&error];
    
    if (!([[inputField stringValue]isEqualTo:regex])) {
            NSLog(@"Found a match");
    // Do stuff in here //
    }
    
    0 讨论(0)
  • 2021-02-05 01:41

    The easiest way to do it.

    Create new NSString without " " (spaces)

    NSString *textWithoutSpaces = [self.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    

    Now you have string without spaces. You just have to check is this string empty or not.

    if (textWithoutSpaces != 0) {
     /* not empty - do something */
    } else {
      /* empty - do something */
    } 
    
    0 讨论(0)
提交回复
热议问题