如何检查文本字段是否为空或不快速
我正在处理下面的代码来检查textField1和textField2文本字段是否有任何input。 
 当我按下button时, IF语句没有做任何事情。 
  @IBOutlet var textField1 : UITextField = UITextField() @IBOutlet var textField2 : UITextField = UITextField() @IBAction func Button(sender : AnyObject) { if textField1 == "" || textField2 == "" { //then do something } } 
	
 简单地比较textfield 对象和空string""并不是正确的方法。 您必须比较text字段的text属性,因为它是一个兼容的types,并保存您正在查找的信息。 
 @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 } 
Swift 3.0:
守卫 :
 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 } 
更好,更美丽的使用
  @IBAction func Button(sender: AnyObject) { if textField1.text.isEmpty || textField2.text.isEmpty { } } 
另一种实时检查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 } } 
如果让…哪里… {
Swift 3 :
 if let _text = theTextField.text, _text.isEmpty { // _text is not empty here } 
Swift 2 :
 if let theText = theTextField.text where !theTextField.text!.isEmpty { // theText is not empty here } 
守卫…在哪里…其他{
 您也可以使用关键字guard : 
Swift 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)") 
Swift 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)") 
 例如,这对于validation链尤其重要。 你可以写一个guard let每个validation和返回或抛出一个exception,如果有一个严重的错误。 
Swift 2 / Xcode 7的小巧gem
 @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 } } 
也许我太晚了,但是我们不能像这样检查:
  @IBAction func Button(sender: AnyObject) { if textField1.text.utf16Count == 0 || textField2.text.utf16Count == 0 { } } 
好吧,这可能会晚,但在Xcode 8我有一个解决scheme:
 if(textbox.stringValue.isEmpty) { // some code } else { //some code } 
现在在swift 3 / xcode 8文本属性是可选的,你可以这样做:
 if ((textField.text ?? "").isEmpty) { // is empty } 
要么:
 if (textField.text?.isEmpty ?? true) { // is empty } 
或者,您可以在下面进行扩展,然后使用它:
 extension UITextField { var isEmpty: Bool { return text?.isEmpty ?? true } } ... if (textField.isEmpty) { // is empty } 
我只是试图用简单的代码向你展示解决scheme
 @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 } } 
这是太晚了,它在Xcode 7.3.1工作正常
 if _txtfield1.text!.isEmpty || _txtfield2.text!.isEmpty { //is empty } 
 我使用了UIKeyInput的内置functionhasText : docs 
对于Swift 2.3,我不得不使用它作为一个方法,而不是一个属性(因为它是在文档中引用):
 if textField1.hasText() && textField2.hasText() { // both textfields have some text } 
简单的方法来检查
 if TextField.stringValue.isEmpty { }