如何在swift中检查文本字段是否为空

Gok*_*lek 50 textbox swift

我正在处理下面的代码,检查textField1textField2文本字段是否有任何输入.

IF当我按下按钮时,声明没有做任何事情.

 @IBOutlet var textField1 : UITextField = UITextField()
 @IBOutlet var textField2 : UITextField = UITextField()
 @IBAction func Button(sender : AnyObject) 
  {

    if textField1 == "" || textField2 == "" 
      {

  //then do something

      }  
  }
Run Code Online (Sandbox Code Playgroud)

Bri*_*acy 158

简单地将textfield 对象与空字符串进行""比较并不是解决此问题的正确方法.您必须比较textfield的text属性,因为它是兼容类型并保存您要查找的信息.

@IBAction func Button(sender: AnyObject) {
    if textField1.text == "" || textField2.text == "" {
        // either textfield 1 or 2's text is empty
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.0:

警卫:

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
Run Code Online (Sandbox Code Playgroud)

如果:

if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
Run Code Online (Sandbox Code Playgroud)

Swift 3.0:

警卫:

guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
Run Code Online (Sandbox Code Playgroud)

如果:

if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
Run Code Online (Sandbox Code Playgroud)

  • textfield中的空格:`if let text = descriptionLabel.text,!text.trimmingCharacters(in:CharacterSet.whitespacesAndNewlines).isEmpty {` (9认同)
  • 来自Java和学习迅速,"守卫"似乎很残酷 (4认同)
  • 文本域中有两个空格? (2认同)
  • @ chrisl08为什么?在某些情况下(例如在验证链中,请查看此页面中的第一个示例:https://thatthinginswift.com/guard-statement-swift/) (2认同)

UnR*_*ewa 48

使用更美好,更美观

 @IBAction func Button(sender: AnyObject) {
    if textField1.text.isEmpty || textField2.text.isEmpty {

    }
}
Run Code Online (Sandbox Code Playgroud)


rap*_*ael 11

另一种检查实时textField源的方法:

 @IBOutlet var textField1 : UITextField = UITextField()

 override func viewDidLoad() 
 {
    ....
    self.textField1.addTarget(self, action: Selector("yourNameFunction:"), forControlEvents: UIControlEvents.EditingChanged)
 }

 func yourNameFunction(sender: UITextField) {

    if sender.text.isEmpty {
      // textfield is empty
    } else {
      // text field is not empty
    }
  }
Run Code Online (Sandbox Code Playgroud)


Ale*_* G. 5

如果让......在...... {

斯威夫特3:

if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特2:

if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
Run Code Online (Sandbox Code Playgroud)

警卫......在哪里......其他{

您还可以使用关键字guard:

斯威夫特3:

guard let theText = theTextField.text where theText.isEmpty else {
    // theText is empty
    return // or throw
}

// you can use theText outside the guard scope !
print("user wrote \(theText)")
Run Code Online (Sandbox Code Playgroud)

斯威夫特2:

guard let theText = theTextField.text where !theTextField.text!.isEmpty else {
    // the text is empty
    return
}

// you can use theText outside the guard scope !
print("user wrote \(theText)")
Run Code Online (Sandbox Code Playgroud)

这对于验证链来说尤其有用,例如在表单中.guard let如果出现严重错误,您可以为每个验证编写一个并返回或抛出异常.


Les*_*ary 5

就像现在swift 3/xcode 8文本属性是可选的,你可以这样做:

if ((textField.text ?? "").isEmpty) {
    // is empty
}
Run Code Online (Sandbox Code Playgroud)

要么:

if (textField.text?.isEmpty ?? true) {
    // is empty
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以进行如下所示的扩展,并使用它:

extension UITextField {
    var isEmpty: Bool {
        return text?.isEmpty ?? true
    }
}

...

if (textField.isEmpty) {
    // is empty
}
Run Code Online (Sandbox Code Playgroud)

  • 考虑到Swift应该简化事情的想法,这似乎是苹果采取的一个不同的途径.认真.;) (2认同)
  • 我会说“text?.isEmpty ??” false` 和 `text?.isEmpty == true` 是不正确的,因为在这两种情况下,您都假设 text 不能为 nil,这是错误的假设(请参阅 Apple 在 UITextField 中的定义:`open var text: String? // default is nil`)所以苹果实际上在这里声明这可以为零。目前在实践中它不是一个零,但将来可能会改变,特别是如果苹果在他们的代码中添加这样的注释。因此,如果文本为零,您应该将其视为文本字段为空。使用 hasText 属性很好,但它仅适用于 iOS 10.0+ (2认同)

Dan*_*anu 5

使用这个扩展

extension String {
    func isBlankOrEmpty() -> Bool {

      // Check empty string
      if self.isEmpty {
          return true
      }
      // Trim and check empty string
      return (self.trimmingCharacters(in: .whitespaces) == "")
   }
}
Run Code Online (Sandbox Code Playgroud)

像这样

// Disable the Save button if the text field is empty.
let text = nameTextField.text ?? ""
saveButton.isEnabled = !text.isBlankOrEmpty()
Run Code Online (Sandbox Code Playgroud)