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

如何检查swift中的文本字段是否为空,swift,textbox,Swift,Textbox,我正在处理下面的代码,以检查textField1和textField2文本字段中是否有任何输入 按下按钮时,IF语句没有执行任何操作 @IBOutlet var textField1 : UITextField = UITextField() @IBOutlet var textField2 : UITextField = UITextField() @IBAction func Button(sender : AnyObject) { if textField1 == "

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

按下按钮时,
IF
语句没有执行任何操作

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

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

  //then do something

      }  
  }

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

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

防护装置

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
如果

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
Swift 3.0:

防护装置

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
如果

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")

更好更美观的使用

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

    }
}

也许我有点晚了,但我们不能这样检查一下:

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

       }
    }
if ((textField.text ?? "").isEmpty) {
    // is empty
}

我只是想用一个简单的代码向您展示解决方案

@IBAction func Button(sender : AnyObject) {
 if textField1.text != "" {
   // either textfield 1 is not empty then do this task
 }else{
   //show error here that textfield1 is empty
 }
}

签入实时文本字段源的另一种方法:

 @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
    }
  }

Swift 2/Xcode 7的小巧宝石

@IBAction func SubmitAgeButton(sender: AnyObject) {

    let newAge = String(inputField.text!)        

if ((textField.text?.isEmpty) != false) {
        label.text = "Enter a number!"
    }
    else {
        label.text = "Oh, you're \(newAge)"

        return
    }

    }
如果让。。。哪里{ Swift 3

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
Swift 2

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
守卫…还有什么地方{ 您还可以使用关键字
guard

Swift 3

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
Swift 2

guard let text = descriptionLabel.text where !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text where !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
guard let text = descriptionLabel.text, !text.isEmpty else {
    return
}
text.characters.count  //do something if it's not empty
if let text = descriptionLabel.text, !text.isEmpty
{
    //do something if it's not empty  
    text.characters.count  
}
if let _text = theTextField.text, _text.isEmpty {
    // _text is not empty here
}
if let theText = theTextField.text where !theTextField.text!.isEmpty {
    // theText is not empty here
}
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)")
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)")
这对于验证链尤其适用,例如表单。您可以为每次验证编写一个
保护let
,如果出现严重错误,则返回或抛出异常。

检查的简单方法

if TextField.stringValue.isEmpty {

}

太晚了,在Xcode 7.3.1中工作正常

if _txtfield1.text!.isEmpty || _txtfield2.text!.isEmpty {
        //is empty
    }

好的,这可能已经晚了,但在Xcode 8中我有一个解决方案:

if(textbox.stringValue.isEmpty) {
    // some code
} else {
    //some code
}

我使用了
uikeyinport
的内置功能
hasText

对于Swift 2.3,我不得不将其作为一种方法而不是一种属性使用(因为它在文档中被引用):


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

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

       }
    }
if ((textField.text ?? "").isEmpty) {
    // is empty
}
或:

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

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

...

if (textField.isEmpty) {
    // is empty
}

Swift 4.x解决方案


Swift 4/xcode 9
Swift 4.2

您可以为每个文本字段使用通用函数,只需在基本控制器中添加以下函数

// White space validation.
func checkTextFieldIsNotEmpty(text:String) -> Bool
{
    if (text.trimmingCharacters(in: .whitespaces).isEmpty)
    {
        return false

    }else{
        return true
    }
}
使用此扩展名

extension String {
    func isBlankOrEmpty() -> Bool {

      // Check empty string
      if self.isEmpty {
          return true
      }
      // Trim and check empty string
      return (self.trimmingCharacters(in: .whitespaces) == "")
   }
}
像这样

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


textfield中有两个空格是什么?textfield中的空格:
如果let text=descriptionLabel.text,!text.trimmingCharacters(在:CharacterSet.whitespaces和newlines中)。我是空的{
来自Java,正在学习swift,“guard”似乎是atrocious@chrisl08为什么?有时候很好(例如,在验证链中,查看本页的第一个示例:)在Swift 2中不再工作,因为
text
属性是可选的。请检查下面的我的答案。
text属性
是可选的。在它之后添加:
if textField1.text!。isEmpty | textField2.text!。isEmpty
…请记住,如果该字段是必需的,用户可以输入空格,如果有必要,您应该检查此条件注意,我们已经收到报告说这在XCode 8中不起作用。你也可以尝试textField.text.isemptyth这已经在Swift 3
中更改为一个实例属性。hasText
问题是用户可以添加简单的空格。鉴于Swift应该简化事情,这似乎是苹果采取的一种不同的方式。Seri
text?.isEmpty??true
是错误的。它只打开text属性,但结果(
true
)将永远不会被调用。正确的语法是
text?.isEmpty
text?.isEmpty==true
。我更喜欢后者,但在这种特殊情况下,text永远不会返回nil。您可以强制将其展开
返回text!。isEmpty
我会说
text?.isEmpty
text?.isEmpty==true
是不正确的,因为在这两种情况下,您都假设文本不能为零,这是错误的假设(请参阅UITextField中的Apple定义:
open var text:String?//默认值为零
)因此,苹果在这里实际上声明这可以是零。实际上,目前它不是零,但将来可能会改变,特别是如果苹果在其代码中添加了这样的注释。因此,如果文本为零,你应该将其视为文本字段为空。使用hasText属性可以,但它仅适用于iOS 10.0+如何回答这个问题@rollstuhlfahrer,谢谢!当我第一次给出答案时,我已经给出了最新Swift版本的答案。谢谢指出。希望这个答案有帮助。单行代码:
return(text.trimmingCharacters(in:.whitespaces.isEmpty)?false:true
为什么不直接返回
trimmingCharacters(in:.whitespaces.isEmpty)。isEmpty